-
Notifications
You must be signed in to change notification settings - Fork 51
/
random-access.suite.js
79 lines (72 loc) · 1.74 KB
/
random-access.suite.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const shuffle = require("knuth-shuffle").knuthShuffle;
const Suite = require("./default-suite").Suite;
const Immutable = require("immutable");
const Denque = require("denque");
const List = require("../../dist/index");
require("../../dist/methods");
const OldList = require("./list-old/dist/index");
const Finger = require("@paldepind/finger-tree");
const { Cons } = require("./list");
const n = 10000;
let indices = [];
let array = [];
let denque = new Denque();
let immut = new Immutable.List();
let tree = Finger.nil;
let list = List.empty();
let oldList = OldList.empty();
for (let i = 0; i < n; ++i) {
tree = Finger.append(i, tree);
list = list.append(i);
oldList = oldList.append(i);
denque.push(i);
array.push(i);
indices.push(i);
immut = immut.push(i);
}
const result = 49995000;
shuffle(indices);
module.exports = Suite("random access")
.add("Array", function() {
let sum = 0;
for (let i = 0; i < n; ++i) {
sum += array[i];
}
return sum === result;
})
.add("Immutable.js", function() {
let sum = 0;
for (let i = 0; i < n; ++i) {
sum += immut.get(i);
}
return sum === result;
})
.add("Deque", function() {
let sum = 0;
for (let i = 0; i < n; ++i) {
sum += denque.peekAt(i);
}
return sum === result;
})
.add("List", function() {
let sum = 0;
for (let i = 0; i < n; ++i) {
sum += list.nth(i);
}
return sum === result;
})
.add("Old list", function() {
let sum = 0;
for (let i = 0; i < n; ++i) {
sum += oldList.nth(i);
}
return sum === result;
})
.add("Finger", function() {
let sum = 0;
for (let i = 0; i < n; ++i) {
sum += Finger.get(i, tree);
}
return sum === result;
})
.run({ async: true });