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 Node = (name)=>({name, children:[], push(...args){this.children.push(...args)}});
const root = Node("root");
const a = Node("a");
const b = Node("b");
const c = Node("c");
const d = Node("d");
const e = Node("e");
const f = Node("f");
const g = Node("g");
const h = Node("h");
root.push(a);
root.push(b);
a.push(c);
a.push(d);
d.push(e);
d.push(f);
e.push(g);
b.push(h);
function dfsExplicit(node, func) {
const stack = [[0,[node]]];
const NEXT_INDEX = 0;
const CHILDREN = 1;
while (stack.length > 0) {
const lastDepth = stack[stack.length-1];
const nextIndex = lastDepth[NEXT_INDEX];
const children = lastDepth[CHILDREN];
if (nextIndex === children.length) {
stack.pop();
continue;
}
const currentNode = children[nextIndex];
func(currentNode);
if (currentNode.children.length > 0) {
stack.push([0, currentNode.children]);
}
lastDepth[NEXT_INDEX] += 1;
}
}
function dfsImplicit(node, func) {
func(node);
for (let i = 0; i < node.children.length; i++) {
dfsImplicit(node.children[i], func);
}
}
class Stack {
constructor() {
this.head = null;
this.stackSize = 0;
}
push(children) {
const newNode = {
nextIndex: 0,
children,
next: this.head
};
this.head = newNode;
this.stackSize++;
}
pop() {
const value = this.head;
this.head = this.head.next;
this.stackSize--;
return value;
}
}
function dfsExplicitStack(node, func, Stack) {
const stack = new Stack();
stack.push([node]);
while (stack.head) {
const depth = stack.head;
const nextIndex = depth.nextIndex;
const children = depth.children;
if (nextIndex === children.length) {
stack.pop();
continue;
}
const currentNode = children[nextIndex];
func(currentNode);
if (currentNode.children.length > 0) {
stack.push(currentNode.children);
}
depth.nextIndex += 1;
}
}
Ready to run.
Test | Ops/sec | |
---|---|---|
dfsExplicit |
| ready |
dfsImplicit |
| ready |
dfsExplicitStack |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.