forked from ryanpcmcquen/flatmap-fast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
43 lines (35 loc) · 1.09 KB
/
test.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
'use strict';
const testArr = ['Hi', 'World'];
const splitWord = (word) => word.split('');
const flatMapFast = require('./index.js');
/* https://www.npmjs.com/package/flatmap v0.0.3 */
const flatmapjs = function (arr, iter, context) {
var results = [];
if (!Array.isArray(arr)) return results;
arr.forEach(function (value, index, list) {
var res = iter.call(context, value, index, list);
if (Array.isArray(res)) {
results.push.apply(results, res);
} else if (res != null) {
results.push(res);
}
});
return results;
};
const testPerf = require('testperf');
testPerf("flatMapFast", flatMapFast, testArr, splitWord);
console.log(
flatMapFast(testArr, splitWord)
);
testPerf("flatmapjs", flatmapjs, testArr, splitWord);
console.log(
flatmapjs(testArr, splitWord)
);
testPerf("flatMapFast", flatMapFast, [1, 2, 3, 4], (x) => [x, x * 2]);
console.log(
flatMapFast([1, 2, 3, 4], (x) => [x, x * 2])
);
testPerf("flatmapjs", flatmapjs, [1, 2, 3, 4], (x) => [x, x * 2]);
console.log(
flatmapjs([1, 2, 3, 4], (x) => [x, x * 2])
);