Skip to content

Instantly share code, notes, and snippets.

@tamaina
Last active February 10, 2019 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tamaina/38e2750c2c2aa48fc97cc5fb1a4825ff to your computer and use it in GitHub Desktop.
Save tamaina/38e2750c2c2aa48fc97cc5fb1a4825ff to your computer and use it in GitHub Desktop.
for (i) vs for-of vs forEach (vs map)

node.jsでforを使うとき、どのようにすると速いのか?実験してみた。

A: [0, 1, 2, …n] から x=-1+0+1+…(n-1) を作る

配列から結果となる数値の計算をすることについて、関数の作成を含めて評価を行う。

  1. for(i) 直接 const有
  2. for(i) 関数 const有
  3. for(i) 直接 const無
  4. for(i) 関数 const無
  5. for-of 直接
  6. for-of 関数
  7. forEach 関数直接
  8. forEach 関数別定義

B: [0, 1, 2, …n] から [-1, 0, 1, …(n-1)] を作る

配列から新たな配列を生成することについて、関数の作成を含めず評価を行う。

  1. for(i) 直接
  2. for(i) 関数
  3. for-of 直接
  4. for-of 関数
  5. forEach 直接
  6. forEach 関数
  7. map 関数直接
  8. map 関数別定義
const newDate = () => new Date()
const length = 10000000
const numbers = Array.from({length}, (v, k) => k)
const howmanytimes = 12
const silent = true
const resarr = []
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有')
{
let $ = 0
const startDate = newDate()
for (let i = 0; i < numbers.length; i += 1) {
const e = numbers[i]
$ += e - 1
}
const time = newDate() - startDate
if (!silent) console.log(`${x}: ${time}`)
msarr.push(time)
}
x += 1
if (!silent) console.log('\nfor(i) 関数 const有')
{
let $ = 0
const startDate = newDate()
const fnc = (e) => $ += e - 1
for (let i = 0; i < numbers.length; i += 1) {
const e = numbers[i]
fnc(e)
}
const time = newDate() - startDate
if (!silent) console.log(`${x}: ${time}`)
msarr.push(time)
}
x += 1
if (!silent) console.log('\nfor(i) 直接 const無')
{
let $ = 0
const startDate = newDate()
for (let i = 0; i < numbers.length; i += 1) {
$ += 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無')
{
let $ = 0
const startDate = newDate()
const fnc = (e) => $ += e - 1
for (let i = 0; i < numbers.length; i += 1) {
fnc(numbers[i])
}
const time = newDate() - startDate
if (!silent) console.log(`${x}: ${time}`)
msarr.push(time)
}
x += 1
if (!silent) console.log('\nfor-of 直接')
{
let $ = 0
const startDate = newDate()
for (const e of numbers) {
$ += e - 1
}
const time = newDate() - startDate
if (!silent) console.log(`${x}: ${time}`)
msarr.push(time)
}
x += 1
if (!silent) console.log('\nfor-of 関数')
{
let $ = 0
const startDate = newDate()
const fnc = (e) => $ += e - 1
for (const e of numbers) {
fnc()
}
const time = newDate() - startDate
if (!silent) console.log(`${x}: ${time}`)
msarr.push(time)
}
x += 1
if (!silent) console.log('\nforEach 関数直接')
{
let $ = 0
const startDate = newDate()
numbers.forEach((e) => $ += e - 1)
const time = newDate() - startDate
if (!silent) console.log(`${x}: ${time}`)
msarr.push(time)
}
x += 1
if (!silent) console.log('\nfor-of 関数別定義')
{
let $ = 0
const startDate = newDate()
const fnc = (e) => $ += e - 1
for (const e of numbers) {
fnc(e)
}
const time = newDate() - startDate
if (!silent) console.log(`${x}: ${time}`)
msarr.push(time)
}
x += 1
return msarr
}
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment