-
Notifications
You must be signed in to change notification settings - Fork 11
/
benchmark.js
61 lines (57 loc) · 1.45 KB
/
benchmark.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
'use strict'
var nul = process.platform === 'win32' ? '\\\\.\\NUL' : '/dev/null'
var bench = require('fastbench')
var stream = require('fs').createWriteStream(nul)
var flatstr = require('./')
var largeStr = JSON.stringify(require('./package.json'))
largeStr += largeStr
largeStr += largeStr
var run = bench([
function unflattenedManySmallConcats (cb) {
stream.write(makeStr('a', 200))
setImmediate(cb)
},
function flattenedManySmallConcats (cb) {
stream.write(flatstr(makeStr('a', 200)))
setImmediate(cb)
},
function unflattenedSeveralLargeConcats (cb) {
stream.write(makeStr(largeStr, 10))
setImmediate(cb)
},
function flattenedSeveralLargeConcats (cb) {
stream.write(flatstr(makeStr(largeStr, 10)))
setImmediate(cb)
},
function unflattenedExponentialSmallConcats (cb) {
stream.write(makeExpoStr('a', 12))
setImmediate(cb)
},
function flattenedExponentialSmallConcats (cb) {
stream.write(flatstr(makeExpoStr('a', 12)))
setImmediate(cb)
},
function unflattenedExponentialLargeConcats (cb) {
stream.write(makeExpoStr(largeStr, 7))
setImmediate(cb)
},
function flattenedExponentialLargeConcats (cb) {
stream.write(flatstr(makeExpoStr(largeStr, 7)))
setImmediate(cb)
}
], 10000)
run(run)
function makeStr (str, concats) {
var s = ''
while (concats--) {
s += str
}
return s
}
function makeExpoStr (str, concats) {
var s = str
while (concats--) {
s += s
}
return s
}