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
Recursive vs Iterative vs JSON.stringify/decode vs JSON replacer
function clone_recursive(object) {
var result = {};
for (var key in object) {
var value = object[key];
if (typeof value === 'object') {
result[key] = clone_recursive(value);
} else {
result[key] = value;
}
}
return result;
}
function clone_iterative(object) {
var result = {};
var stack = [result, object];
var curr, base;
while (curr = stack.pop()) {
var base = stack.pop();
for (var key in curr) {
var value = curr[key];
if (typeof value === 'object') {
stack.push(base[key] = {});
stack.push(value)
} else {
base[key] = value;
}
}
}
return result;
}
function clone_iterative_linkedList(object) {
var result = {};
var item = {
base: result,
value: object
};
var last = item;
do {
var current = item.value;
var base = item.base;
for (var key in current) {
var value = current[key];
if (typeof value === 'object') {
var resultValue = base[key] = {};
last.next = {
base: resultValue,
value: value
};
last = last.next;
} else {
base[key] = value;
}
}
} while (item = item.next);
return result;
}
function clone_JSON(o) {
return JSON.parse(JSON.stringify(o));
}
function clone_JSON_replacer(x) {
var r = {},
lastSrc, lastDst = r,
stack = [],
v;
function replacer(key, value) {
while (this !== lastSrc && stack.length) {
lastDst = stack.pop();
lastSrc = stack.pop();
}
if (typeof value === "object") {
stack.push(lastSrc, lastDst);
lastDst[key] = v = new value.constructor;
lastDst = v;
lastSrc = value;
return value;
} else {
lastDst[key] = value;
return undefined;
}
}
JSON.stringify(x, replacer);
return r[""];
}
function makeDeepObject() {
var root = {};
var current = root;
for (var i = 33, max = 136; i < max; i++) {
var key = String.fromCharCode(i);
var newObject = {};
for (var j = 0; j < 25; j++) {
newObject['a' + j] = {
x: 1
};
}
current[key] = newObject;
for (; j < 50; j++) {
newObject['z' + j] = {
y: 2
};
}
current = newObject;
}
return root;
}
var mock = makeDeepObject();
Ready to run.
Test | Ops/sec | |
---|---|---|
Recursive |
| ready |
Iterative: Array |
| ready |
Iterative: linkedList |
| ready |
JSON |
| ready |
JSON replacer |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.