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
Fastest way to dereference an object property path stored in a string.
function index(obj, i) {
return obj[i]
}
var obj = {
a: {
b: {
x: 0
}
}
};
function deref2(obj, s) {
var i = 0;
var bits = s.split('.')
while (obj && i < bits.length)
obj = obj[bits[i++]];
return obj;
}
var cache = {}
function dynamicFnWithCache(object, path) {
var fn = cache[path]
if (!fn) {
fn = cache[path] = new Function('arg', 'return arg.' + path)
}
return fn.call(null, object)
}
function deref2Arr(obj, bits) {
var i = 0;
while (obj && i < bits.length)
obj = obj[bits[i++]];
return obj;
}
var cache2 = {}
function dynamicFnWithCacheArr(object, p) {
var path = p.join('.')
var fn = cache2[path]
if (!fn) {
fn = cache2[path] = new Function('arg', 'return arg.' + path)
}
return fn.call(null, object)
}
var cache3 = {}
function dynamicFnWithCacheArrToString(object, p) {
var path = p.toString()
var fn = cache3[path]
if (!fn) {
fn = cache3[path] = new Function('arg', 'return arg.' + p.join('.'))
}
return fn.call(null, object)
}
function getIn(obj, bits) {
for(var i = 0, j = bits.length; i < j; ++i) {
obj = obj[bits[i]];
}
return obj;
}
Ready to run.
Test | Ops/sec | |
---|---|---|
deref loop 2 |
| ready |
dynamicFnWithCache |
| ready |
deref loop 2 arr |
| ready |
dynamicFnWithCacheArr |
| ready |
dynamicFnWithCacheArrToString |
| ready |
getIn |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.