JavaScript Node #1 — Binary Trees
Allocate, walk and deallocate many perfect binary trees, while one long-lived tree stays alive throughout.
| Time | 6,299.5 ms |
|---|---|
| CPU time | 9,921.7 ms |
| Peak memory | 1,188,564 KB |
| gz | 448 bytes — comments removed, gzipped |
| Style | ★★★★☆ |
| Implementation | JavaScript — JavaScript (Node.js) 24.10.0 |
| By | sysop- |
| Submitted | September 24, 2026 |
Style assessment
This is the canonical ES5 Benchmarks Game style: prototype-based TreeNode, recursive bottomUpTree/itemCheck, clear domain names (stretchDepth, longLivedTree), and idiomatic touches like +process.argv[2] and 1 << for the iteration count. Minor deductions for the dead unused `item` constructor parameter, var-based module-level script flow with no main function, and pre-ES6 syntax (const/let, class, template literals) that modern Node code would use.
Source
51 lines · Download binary-trees-javascript-node-1.js
/* The Computer Language Benchmarks Game
http://benchmarksgame.alioth.debian.org/
contributed by Isaac Gouy
*reset*
*/
function TreeNode(left,right,item){
this.left = left;
this.right = right;
}
TreeNode.prototype.itemCheck = function(){
if (this.left==null) return 1;
else return 1 + this.left.itemCheck() + this.right.itemCheck();
}
function bottomUpTree(depth){
if (depth>0){
return new TreeNode(
bottomUpTree(depth-1)
,bottomUpTree(depth-1)
);
}
else {
return new TreeNode(null,null);
}
}
var minDepth = 4;
var n = +process.argv[2];
var maxDepth = Math.max(minDepth + 2, n);
var stretchDepth = maxDepth + 1;
var check = bottomUpTree(stretchDepth).itemCheck();
console.log("stretch tree of depth " + stretchDepth + "\t check: " + check);
var longLivedTree = bottomUpTree(maxDepth);
for (var depth=minDepth; depth<=maxDepth; depth+=2){
var iterations = 1 << (maxDepth - depth + minDepth);
check = 0;
for (var i=1; i<=iterations; i++){
check += bottomUpTree(depth).itemCheck();
}
console.log(iterations + "\t trees of depth " + depth + "\t check: " + check);
}
console.log("long lived tree of depth " + maxDepth + "\t check: "
+ longLivedTree.itemCheck());