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
function index(obj, i) {
return obj[i]
}
var obj = {
a: {
b: {
x: 0
}
}
};
function deref(obj, s) {
var i = 0;
s = s.split('.');
while (obj && i < s.length)
obj = obj[s[i++]];
return obj;
}
//I left this here out of deference to the OP, but it didn't return correct result when I tested it locally - KC
function derefrecursive(obj, s, i) {
if (i === undefined) i = 0;
if (i < s.length) return derefrecursive(obj[s[i]], s, i + 1);
return obj;
}
//Function constructor method -- optimized for repeatedly dereferencing the same path (memoizedLookupFunction). Performs slower than eval() if you aren't reusing the path (lookupFunction).
function lookupFunction(obj, path) {
var func = Function("obj", "return obj." + path + ";");
return func(obj);
}
var derefFuncs = {};
function memoizedLookupFunction(obj, path) {
if (!derefFuncs[path]) {
derefFuncs[path] = Function("obj", "return obj." + path + ";");
}
return derefFuncs[path](obj);
}
Ready to run.
Test | Ops/sec | |
---|---|---|
split + reduce |
| ready |
eval |
| ready |
deref loop |
| ready |
deref recursive |
| ready |
worst case lookup function |
| ready |
best case memoized lookup function |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.