|
const newDate = () => new Date() |
|
|
|
const length = 5000000 |
|
const numbers = Array.from({length}, (v, k) => k) |
|
|
|
const howmanytimes = 12 |
|
const silent = true |
|
|
|
const resarr = [] |
|
|
|
const fnc = (e, i, arr) => e - 1 |
|
|
|
if (!silent) console.log(`length: ${length}`) |
|
|
|
for (let n = 1; n <= howmanytimes; n += 1) { |
|
resarr.push(doit(n)) |
|
} |
|
|
|
const fs = require('fs') |
|
let csv = "" |
|
resarr.forEach((arr) => { |
|
csv += `${arr.join(",")}\n` |
|
}) |
|
|
|
const filename = `result.${newDate().getTime()}.csv` |
|
|
|
fs.writeFile(filename, csv, (err) => { |
|
if (err) throw Error(err) |
|
if (!silent) console.log(`result written as ${filename}`) |
|
return |
|
}) |
|
|
|
function doit(whattime) { |
|
if (!silent) console.log(`[[ ${whattime} ]]`) |
|
let x = 1 |
|
const msarr = [] |
|
if (!silent) console.log('\nfor(i) 直接') |
|
{ |
|
const $ = [] |
|
const startDate = newDate() |
|
|
|
for (let i = 0; i < numbers.length; i += 1) { |
|
$.push(numbers[i] - 1) |
|
} |
|
|
|
const time = newDate() - startDate |
|
if (!silent) console.log(`${x}: ${time}`) |
|
msarr.push(time) |
|
} |
|
x += 1 |
|
|
|
if (!silent) console.log('\nfor(i) 関数') |
|
{ |
|
const $ = [] |
|
const startDate = newDate() |
|
|
|
for (let i = 0; i < numbers.length; i += 1) { |
|
$.push(fnc(numbers[i], i, numbers)) |
|
} |
|
|
|
const time = newDate() - startDate |
|
if (!silent) console.log(`${x}: ${time}`) |
|
msarr.push(time) |
|
} |
|
x += 1 |
|
|
|
if (!silent) console.log('\nfor-of 直接') |
|
{ |
|
const $ = [] |
|
const startDate = newDate() |
|
|
|
for (const e of numbers) { |
|
$.push(e - 1) |
|
} |
|
|
|
const time = newDate() - startDate |
|
if (!silent) console.log(`${x}: ${time}`) |
|
msarr.push(time) |
|
} |
|
x += 1 |
|
|
|
if (!silent) console.log('\nfor-of 関数') |
|
{ |
|
const $ = [] |
|
const startDate = newDate() |
|
|
|
for (const e of numbers) { |
|
$.push(fnc(e, null, numbers)) |
|
} |
|
|
|
const time = newDate() - startDate |
|
if (!silent) console.log(`${x}: ${time}`) |
|
msarr.push(time) |
|
} |
|
x += 1 |
|
if (!silent) console.log('\nforEach 直接') |
|
{ |
|
const $ = [] |
|
const startDate = newDate() |
|
|
|
numbers.forEach((e) => $.push(e - 1)) |
|
|
|
const time = newDate() - startDate |
|
if (!silent) console.log(`${x}: ${time}`) |
|
msarr.push(time) |
|
} |
|
x += 1 |
|
|
|
if (!silent) console.log('\nforEach 関数') |
|
{ |
|
const $ = [] |
|
const startDate = newDate() |
|
|
|
numbers.forEach((e, i, arr) => $.push(fnc(e, i , arr))) |
|
|
|
const time = newDate() - startDate |
|
if (!silent) console.log(`${x}: ${time}`) |
|
msarr.push(time) |
|
} |
|
x += 1 |
|
|
|
if (!silent) console.log('\nmap 関数直接') |
|
{ |
|
const startDate = newDate() |
|
|
|
const $ = numbers.map((e) => e - 1) |
|
|
|
const time = newDate() - startDate |
|
if (!silent) console.log(`${x}: ${time}`) |
|
msarr.push(time) |
|
} |
|
x += 1 |
|
|
|
if (!silent) console.log('\nmap 関数別定義') |
|
{ |
|
const startDate = newDate() |
|
|
|
const $ = numbers.map(fnc) |
|
|
|
const time = newDate() - startDate |
|
if (!silent) console.log(`${x}: ${time}`) |
|
msarr.push(time) |
|
} |
|
x += 1 |
|
|
|
return msarr |
|
} |