Skip to content

Instantly share code, notes, and snippets.

@jebai0521
Created June 8, 2020 06:11
Show Gist options
  • Save jebai0521/56cac8a0381fa8ffd7ef340bc553118e to your computer and use it in GitHub Desktop.
Save jebai0521/56cac8a0381fa8ffd7ef340bc553118e to your computer and use it in GitHub Desktop.
array combin
const a = {
key: 'openingHours',
type: 'openingHours',
placeholder: 'form_opening_hours',
properties: {
// name: 'openingHours',
},
};
console.log(JSON.stringify(a, null, 2));
// const pls = [[ 1, 2, 3 ], [ 3, 4, 5 ], [ 7, 8, 9 ]],
const pls = [];
for (let i = 0; i < 100; i++) {
const pl = [];
for (let j = 0; j < 100; j++) {
pl.push(j);
}
pls.push(pl);
}
function test1() {
const start = new Date(),
ids = pls.reduce((res = [], pl) => res.concat(pl), []);
// console.log(ids, ids.length, (new Date() - start));
console.log('concat', (new Date() - start));
}
function test2() {
const start = new Date(),
ids = pls.reduce((res = [], pl) => { res.push(...pl); return res; }, []);
// console.log(ids, ids.length, (new Date() - start));
console.log('push', (new Date() - start));
}
function test3() {
const start = new Date(),
ids = pls.reduce((res = [], pl) => { res.splice(res.length - 1, 0, ...pl); return res; }, []);
// console.log(ids, ids.length, (new Date() - start));
console.log('splice', (new Date() - start));
}
function test4() {
const start = new Date(),
ids = pls.reduce((res = [], pl) => [ ...res, ...pl ], []);
// console.log(ids, ids.length, (new Date() - start));
console.log('constructor', (new Date() - start));
}
// 1000 * 10000
// test1(); // 28681 ms
// test2(); // 304
// test3(); // 208
// test4(); // 66134 ms
// 1000 * 100
test1(); // 28681 ms
test2(); // 304
test3(); // 208
test4(); // 66134 ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment