Skip to content

Commit 9e65a9e

Browse files
committed
Add class benchmarks
1 parent 369d062 commit 9e65a9e

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

benchmarks/treenode-array-vs-object-creation.js

+19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
var Benchmark = require('benchmark');
22

3+
function Node(k, v, p, l, r) {
4+
this.key = k;
5+
this.value = v;
6+
this.parent = p;
7+
this.left = l;
8+
this.right = r;
9+
}
10+
311
new Benchmark.Suite()
412
.add('Object node creation', function() {
513
var parent = {
@@ -41,9 +49,20 @@ new Benchmark.Suite()
4149
left[2] = parent;
4250
right[2] = parent;
4351
})
52+
.add('Class node creation', function() {
53+
var parent = new Node('some_key', 1234123412, null, null, null);
54+
var left = new Node('left_child', 'asdfoasjdfpasdjf', null, null, null);
55+
var right = new Node('right_child', 'qwerqwerqwerqwwq', null, null, null);
56+
57+
parent[3] = left;
58+
parent[4] = right;
59+
left[2] = parent;
60+
right[2] = parent;
61+
})
4462
.on('complete', function() {
4563
console.log(this[0].toString());
4664
console.log(this[1].toString());
65+
console.log(this[2].toString());
4766

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

benchmarks/treenode-array-vs-object-manipulation.js

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
var Benchmark = require('benchmark');
22

3+
function Node(k, v, p, l, r) {
4+
this.k = k;
5+
this.v = v;
6+
this.p = p;
7+
this.l = l;
8+
this.r = r;
9+
}
10+
311
var array = {
412
parent: ['some_key', 1234123412, null, null, null],
513
left: ['left_child', 'asdfoasjdfpasdjf', null, null, null],
@@ -36,6 +44,12 @@ var objectS = {
3644
right: { k: 'right child', v: 'qwerqwerqwerqwwq', p: null, l: null, r: null }
3745
};
3846

47+
var objectC = {
48+
parent: new Node('some_key', 1234123412, null, null, null),
49+
left: new Node('left_child', 'asdfoasjdfpasdjf', null, null, null),
50+
right: new Node('right_child', 'qwerqwerqwerqwwq', null, null, null)
51+
};
52+
3953
new Benchmark.Suite()
4054
.add('Object node manipulation', function() {
4155
var temp = object.parent;
@@ -57,7 +71,7 @@ new Benchmark.Suite()
5771
object.right.right = null;
5872
})
5973
.add('Object node manipulation - small key', function() {
60-
var temp = objectS.parent;
74+
var temp = objectS.parent;
6175
objectS.parent = objectS.left;
6276
objectS.left = objectS.right;
6377
objectS.right = temp;
@@ -75,6 +89,25 @@ new Benchmark.Suite()
7589
objectS.right.l = null;
7690
objectS.right.r = null;
7791
})
92+
.add('Class node manipulation', function() {
93+
var temp = objectC.parent;
94+
objectC.parent = objectC.left;
95+
objectC.left = objectC.right;
96+
objectC.right = temp;
97+
98+
objectC.parent.p = null;
99+
objectC.parent.l = objectC.left;
100+
objectC.parent.r = objectC.right;
101+
102+
103+
objectC.left.p = objectC.parent;
104+
objectC.left.l = null;
105+
objectC.left.r = null;
106+
107+
objectC.right.p = objectC.parent;
108+
objectC.right.l = null;
109+
objectC.right.r = null;
110+
})
78111
.add('Array node manipulation', function() {
79112
var temp = array.parent;
80113
array.parent = array.left;
@@ -97,6 +130,7 @@ new Benchmark.Suite()
97130
console.log(this[0].toString());
98131
console.log(this[1].toString());
99132
console.log(this[2].toString());
133+
console.log(this[3].toString());
100134

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

0 commit comments

Comments
 (0)