Test environment: Node 12.18.2 on Intel(R) Xeon(R) CPU E5-2697 v3 @ 2.60GHz
const SZ = 99999999
const l2a = [];
for (let i = 0; i < 10000; i++) {
const sub = [];
for (let j = 0; j < 10000; j++) {
sub.push(j);
}
l2a.push(sub);
}
let s = new Set();
let t = -Date.now();
for (const a of l2a) {
for (const n of a) {
s.add(n);
}
}
t += Date.now();
console.log('Takes: ', t);
const ta = [];
t = -Date.now();
for (const a of l2a) {
ta.concat(...a);
}
s = new Set(ta);
t += Date.now();
console.log('Takes: ', t);
Result is
Takes: 3070
Takes: 1665
Concating and expanding array are very fast comparing to calling a function(Set.add).
'Language > Javascript' 카테고리의 다른 글
[rxjs] Interesting unexpected behavior of withLatestFrom() at Rxjs. (0) | 2020.04.14 |
---|---|
[NodeJs] Overhead of Promise(async/await) (more serious than you think!) (0) | 2019.05.16 |
[Jest] Sharing code and symbolic link 문제. (0) | 2018.06.26 |
[NodeJS] Kill child process executed via 'exec' and 'spawn' (0) | 2017.12.28 |
[Javascript] Generator 를 이용해서 Callback hell을 벗어나 보기.... (0) | 2017.12.28 |