-
Notifications
You must be signed in to change notification settings - Fork 12
/
vanilla.js
53 lines (48 loc) · 1.56 KB
/
vanilla.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// A comparison with a vanilla Javascript object (and Set):
var HashTable = require('./index.js');
var Node = { crypto: require('crypto') };
var keySize = 16;
var valueSize = 0;
var element = keySize + valueSize;
var elements = 4000000;
var buffer = Node.crypto.randomBytes(element * elements);
console.log('');
console.log(
' '.repeat(23) + 'key=' + keySize + ' bytes, value=' + valueSize + ' bytes'
);
console.log('');
console.log(' @ronomon/hash-table: Inserting ' + elements + ' elements...');
var now = Date.now();
var table = new HashTable(keySize, valueSize, elements, elements);
var offset = 0;
var length = elements;
while (length--) {
table.set(buffer, offset, buffer, offset + keySize);
offset += element;
}
console.log(' @ronomon/hash-table: ' + (Date.now() - now) + 'ms');
console.log('');
console.log(' new Set(): Inserting ' + elements + ' elements...');
var now = Date.now();
var set = new Set();
var offset = 0;
var length = elements;
while (length--) {
set.add(buffer.slice(offset, offset + keySize));
offset += element;
}
console.log(' new Set(): ' + (Date.now() - now) + 'ms');
console.log('');
console.log(' vanilla object: Inserting ' + elements + ' elements...');
var now = Date.now();
var object = {};
var offset = 0;
var length = elements;
while (length--) {
var key = buffer.toString('base64', offset, offset + keySize);
// We don't even try to slice a value (this would be slower), just use "1":
object[key] = 1;
offset += element;
}
console.log(' vanilla object: ' + (Date.now() - now) + 'ms');
console.log('');