jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
<script scr="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>
<script>
var regex = /\${([^}]*)}/g;
function parseTemplateObject(templateObject, context){
var startTime = Date.now();
var {
window,
document,
setTimeout,
setInterval,
currentRecord,
currentUser,
iteratorObject,
router,
parentRecord,
resources,
} = context;
console.log(templateObject,'T');
try{
if (_.isEmpty(templateObject)) {
return templateObject;
}
function recursivelyIterate(object, acc = {}) {
if (!_.isObject(object)) {
return object.replace(regex, (match, group) => eval(`${group}`));
}
_.forEach(object, (value, key) => {
if (_.isObject(value)) {
if (Array.isArray(value)) {
acc[key] = value.map(element => recursivelyIterate(element));
} else {
acc[key] = {}; // currentRecord:{}
//f the property is an object, recursively iterate over its properties
acc[key] = recursivelyIterate(value);
}
} else {
// eslint-disable-next-line no-lonely-if -- legacy code
if (typeof value === 'string') {
//only exp
if (/^\$\{[^}]*\}$/.test(value)) {
acc[key] = eval(value.substring(2, value.length - 1));
} else {
//exp having some string appended or prepended
acc[key] = value.replace(regex, (match, group) => eval(`${group}`));
}
} else {
acc[key] = value;
}
}
});
return acc;
};
return recursivelyIterate(templateObject);
}
catch(e){
console.error(e);
}
};
function templateObject2(
templateObject,
context
){
console.log('test',templateObject,context,_);
if (_.isEmpty(templateObject)) {
return templateObject;
}
try {
return JSON.parse(_.template(JSON.stringify(templateObject))(context)) ??{};
} catch (e) {
return templateObject;
}
};
</script>
Ready to run.
Test | Ops/sec | |
---|---|---|
eval approach |
| ready |
_template |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.