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
const obj = {
"foo": {
"bar": {
"baz": {
"oof": 1
}
}
}
}
function v1(obj, path) {
const pathParts = path.split('.');
let currentObj = obj;
for (const part of pathParts) {
currentObj = currentObj?.[part];
if (currentObj === undefined) {
return undefined;
}
}
return currentObj;
}
function v2(obj, path) {
let currentObj = obj;
let i = 0;
while (i < path.length) {
if (currentObj === null || typeof currentObj !== 'object') {
return undefined;
}
let dotIndex = path.indexOf('.', i);
if (dotIndex === -1) {
dotIndex = path.length;
}
const part = path.substring(i, dotIndex);
if (!(part in currentObj)) {
return undefined;
}
currentObj = currentObj[part];
i = dotIndex + 1;
}
return currentObj;
}
function v3(obj, path) {
const pathParts = path.split('.');
let currentObj = obj;
for (const part of pathParts) {
if (currentObj === null || typeof currentObj !== 'object' || !(part in currentObj)) {
return undefined;
}
currentObj = currentObj[part];
}
return currentObj;
}
function v4(obj, path) {
const pathParts = path.split('.');
let currentObj = obj;
let part;
do {
currentObj = currentObj?.[part];
} while (currentObj !== undefined && (part = partPaths.unshift()));
return currentObj;
}
function v5(obj, path) {
const pathParts = path.split('.');
const length = pathParts.length;
let currentObj = obj;
let i;
while (i < length && currentObj !== undefined) {
currentObj = currentObj?.[partPaths[i]];
}
return currentObj;
}
Ready to run.
Test | Ops/sec | |
---|---|---|
v1 Found Property |
| ready |
v2 Found Property |
| ready |
v3 Found Property |
| ready |
v4 Found Property |
| ready |
v5 Found Property |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.