Override Functions

Benchmark created on


Description

Test for Overrides

Setup

function getOverrides(id, condValue, object) {
            let startTime = performance.now();
            let result = {};
            let workObj = typeof (object) === 'string' ? JSON.parse(object) : object;
            if (workObj.hasOwnProperty(condValue)) {
                // Check, if Property exists - if yes, directly return it, because thats the best match
                result = workObj[condValue];
            } else {
                // Property not found. We need to check the values!
                const operators = new RegExp('[=><!]');
                let tmpObj = Object.keys(workObj)
                    .sort(
                        (a, b) => a.toLowerCase().localeCompare(b.toLowerCase(), undefined, { numeric: true, sensitivity: 'base' }))
                    .reduce(
                        (obj, key) => {
                            // Check, if we have a condition inside the object
                            if (operators.test(key)) {
                                // Now, we need to check, if condValue is a number
                                if (!isNaN(condValue)) {
                                    // Operator found - check for condition
                                    try {
                                        const func = Function(`return ${condValue}${key}`)();
                                        if (func) {
                                            result = workObj[key];
                                        }
                                    }
                                    catch (func) {
                                        console.log(`Overrides for element ${id} can not be processed, as they are not correctly formatted! ${func} For: ${key}`);
                                    }
                                }
                            }
                            return obj;
                        },
                        {}
                    );
            }

            if (Object.keys(result).length == 0) {
                // Check, if we have a fallback for it
                if (workObj.hasOwnProperty('default')) {
                    result = workObj['default'];
                }
            }

            // Process the result values, if available
            if (Object.keys(result).length > 0) {
                // Value
                if (result.hasOwnProperty('value')) {
                    try {
                        const func = new Function(`return ${result['value']}`)();
                        result['value'] = func(condValue);
                    }
                    catch (func) {
                        console.log(`Value-Overrides for element ${id} can not be processed, as they are not correctly formatted! Error: ${func} Supplied function: ${result['value']}`);
                        result['value'] = 0;
                    }
                }

                // Unit
                if (result.hasOwnProperty('unit')) {
                    try {
                        const func = Function(`return ${result['unit']}`)();
                        result['unit'] = func(condValue);
                    }
                    catch (func) {
                        console.log(`Unit-Overrides for element ${id} can not be processed, as they are not correctly formatted! Error: ${func} Supplied function: ${result['unit']}`);
                        result['unit'] = '';
                    }
                }
            }
            let endTime = performance.now();
            console.log(`${Object.keys(object).length} Elements checking took ${endTime - startTime} ms`);
            return result;
        }

Test runner

Ready to run.

Testing in
TestOps/sec
Testing the code
let over = {
            "Winter": {
                "fill": "red",
                "unit": "Keine Ahnung"
            },
            "Sommer": {
                "fill": "rgb(161,211,67)",
            },
            ">=21": {
                "fill": "haha",
                "value": "val => Number(Number(50)+Number(50))+Number(100)+val",
                "unit": 21
            },
            ">=22": {
                "fill": "haha22",
                "value": "val => Number(Number(5)+Number(5))+Number(10)+val",
                "unit": 22
            },
            "==11": {
                "fill": "rgb(47,47,47)",
                "value": "val => parseFloat(val/1000)",
                "unit": "val => val >= 1000 ? 'kW' : 'W'"
            },
            "Bla": {
                "what": "todo"
            },
            0: {
                "hehe": "hehe"
            },
            "default": {
                "value": "val => parseFloat(val/1000).toFixed(2)",
                "fill": "haha",
                "unit": "val => Math.abs(val) > 1000 ? 'kW' : 'W'"
            }
        };
        let that = getOverrides(1, -1200, over);
        console.log(that);
ready
Testing the code 2
let over = {
            "Winter": {
                "fill": "red",
                "unit": "Keine Ahnung"
            },
            "Sommer": {
                "fill": "rgb(161,211,67)",
            },
            ">=21": {
                "fill": "haha",
                "value": "val => Number(Number(50)+Number(50))+Number(100)+val",
                "unit": 21
            },
            ">=22": {
                "fill": "haha22",
                "value": "val => Number(Number(5)+Number(5))+Number(10)+val",
                "unit": 22
            },
            "==11": {
                "fill": "rgb(47,47,47)",
                "value": "val => parseFloat(val/1000)",
                "unit": "val => val >= 1000 ? 'kW' : 'W'"
            },
            "Bla": {
                "what": "todo"
            },
            0: {
                "hehe": "hehe"
            },
            "default": {
                "value": "val => parseFloat(val/1000).toFixed(2)",
                "fill": "haha",
                "unit": "val => Math.abs(val) > 1000 ? 'kW' : 'W'"
            }
        };
        let that = getOverrides(1, 200, over);
        console.log(that);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.