Skip to content

Commit 7a0d3c0

Browse files
committed
Fix performance test.
Speed improvements are definitely not linear. 20% speed improvement on a dual-core cpu seems possible though. Also increases the operation length since the process spawn overhead is pretty big.
1 parent 1dfb130 commit 7a0d3c0

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

test/performance.spec.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
describe('Performance', function () {
22
var isNode = typeof module !== 'undefined' && module.exports;
3-
var cpus = isNode && require('os').cpus().length > 2 ? 2 : 1;
43

54
it('.map() should be using multi-threading (could fail on single-core)', function () {
65
var slowSquare = function (n) {
@@ -10,13 +9,15 @@
109
};
1110

1211
var Thread = require('../lib/thread.js');
13-
var p = new Thread([1000, 2000, 3000]);
14-
var p2 = new Thread([1000, 2000, 3000]);
12+
var p = new Thread([10000, 20000, 30000]);
13+
var p2 = new Thread([10000, 20000, 30000]);
1514

16-
var start = Date.now();
15+
var start;
1716
var time = null;
1817

1918
runs(function () {
19+
start = Date.now();
20+
2021
p.spawn(function (data) {
2122
for (var i = 0; i < data.length; ++i) {
2223
var n = data[i];
@@ -34,10 +35,12 @@
3435
return time !== null;
3536
}, "Sequential should finish", 5000);
3637

37-
var start2 = Date.now();
38+
var start2;
3839
var time2 = null;
3940

4041
runs(function () {
42+
start2 = Date.now();
43+
4144
p2.map(slowSquare).then(function (data) {
4245
time2 = Date.now() - start2;
4346
});
@@ -48,7 +51,7 @@
4851
}, "Parallel should finish", 5000);
4952

5053
runs(function () {
51-
expect(time2).toBeLessThan(time / cpus);
54+
expect(time2).toBeLessThan(time * 0.8);
5255
});
5356
});
5457
});

0 commit comments

Comments
 (0)