Skip to content

Commit

Permalink
Add class benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpods committed Jan 5, 2015
1 parent 369d062 commit 9e65a9e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
19 changes: 19 additions & 0 deletions benchmarks/treenode-array-vs-object-creation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
var Benchmark = require('benchmark');

function Node(k, v, p, l, r) {
this.key = k;
this.value = v;
this.parent = p;
this.left = l;
this.right = r;
}

new Benchmark.Suite()
.add('Object node creation', function() {
var parent = {
Expand Down Expand Up @@ -41,9 +49,20 @@ new Benchmark.Suite()
left[2] = parent;
right[2] = parent;
})
.add('Class node creation', function() {
var parent = new Node('some_key', 1234123412, null, null, null);
var left = new Node('left_child', 'asdfoasjdfpasdjf', null, null, null);
var right = new Node('right_child', 'qwerqwerqwerqwwq', null, null, null);

parent[3] = left;
parent[4] = right;
left[2] = parent;
right[2] = parent;
})
.on('complete', function() {
console.log(this[0].toString());
console.log(this[1].toString());
console.log(this[2].toString());

console.log('Fastest is ' + this.filter('fastest').pluck('name'));
})
Expand Down
36 changes: 35 additions & 1 deletion benchmarks/treenode-array-vs-object-manipulation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
var Benchmark = require('benchmark');

function Node(k, v, p, l, r) {
this.k = k;
this.v = v;
this.p = p;
this.l = l;
this.r = r;
}

var array = {
parent: ['some_key', 1234123412, null, null, null],
left: ['left_child', 'asdfoasjdfpasdjf', null, null, null],
Expand Down Expand Up @@ -36,6 +44,12 @@ var objectS = {
right: { k: 'right child', v: 'qwerqwerqwerqwwq', p: null, l: null, r: null }
};

var objectC = {
parent: new Node('some_key', 1234123412, null, null, null),
left: new Node('left_child', 'asdfoasjdfpasdjf', null, null, null),
right: new Node('right_child', 'qwerqwerqwerqwwq', null, null, null)
};

new Benchmark.Suite()
.add('Object node manipulation', function() {
var temp = object.parent;
Expand All @@ -57,7 +71,7 @@ new Benchmark.Suite()
object.right.right = null;
})
.add('Object node manipulation - small key', function() {
var temp = objectS.parent;
var temp = objectS.parent;
objectS.parent = objectS.left;
objectS.left = objectS.right;
objectS.right = temp;
Expand All @@ -75,6 +89,25 @@ new Benchmark.Suite()
objectS.right.l = null;
objectS.right.r = null;
})
.add('Class node manipulation', function() {
var temp = objectC.parent;
objectC.parent = objectC.left;
objectC.left = objectC.right;
objectC.right = temp;

objectC.parent.p = null;
objectC.parent.l = objectC.left;
objectC.parent.r = objectC.right;


objectC.left.p = objectC.parent;
objectC.left.l = null;
objectC.left.r = null;

objectC.right.p = objectC.parent;
objectC.right.l = null;
objectC.right.r = null;
})
.add('Array node manipulation', function() {
var temp = array.parent;
array.parent = array.left;
Expand All @@ -97,6 +130,7 @@ new Benchmark.Suite()
console.log(this[0].toString());
console.log(this[1].toString());
console.log(this[2].toString());
console.log(this[3].toString());

console.log('Fastest is ' + this.filter('fastest').pluck('name'));
})
Expand Down

0 comments on commit 9e65a9e

Please sign in to comment.