-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy path5-range.js
50 lines (45 loc) · 1.03 KB
/
5-range.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
'use strict';
const benchmark = require('./2-benchmark.js');
const common = require('metarhia-common');
const os = require('os');
const rangePush = (min, max) => {
const arr = [];
for (let i = min; i <= max; i++) arr.push(i);
return arr;
};
const rangeNew = (from, to) => {
if (to < from) return [];
const len = to - from + 1;
const range = new Array(len);
for (let i = from; i <= to; i++) {
range[i - from] = i;
}
return range;
};
const rangeEx = range => {
const from = range[0];
let to = range[1];
const toType = typeof to;
if (toType === 'undefined') {
to = range[2];
return common.range(from, to);
} else if (toType !== 'number') {
let count = to[0];
if (count < 0) {
const cpus = os.cpus().length;
count = cpus + count;
}
return common.range(from, from + count - 1);
}
};
benchmark.do(1000000, [
function testRangePush() {
rangePush(1, 1000);
},
function testRangeNew() {
rangeNew(1, 1000);
},
function testRangeEx() {
rangeEx([1, [1000]]);
},
]);