Skip to content

Instantly share code, notes, and snippets.

@tkfm-yamaguchi
Last active January 1, 2016 08:29
Show Gist options
  • Save tkfm-yamaguchi/8119017 to your computer and use it in GitHub Desktop.
Save tkfm-yamaguchi/8119017 to your computer and use it in GitHub Desktop.
###
# $ coffee -c loop.coffee ; nodejs loop.js
###
measure = (title, f) ->
begin = new Date()
f()
console.log "#{title}: #{(new Date() - begin)}"
times = 10000000
array = [1..times]
# AVG: 395
things = undefined
measure "Array.push", ->
things = []
for factor in array
things.push factor
# To avoid that coffee creates an anonymous array
# for the return value of this function.
return things
# AVG: 360
things = undefined
measure "inline", ->
things = (x for x in array)
###
# $ coffee -c loop.coffee ; nodejs loop.js
###
measure = (title, f) ->
begin = new Date()
f()
console.log "#{title}: #{(new Date() - begin)}"
times = 10000000
array = [1..times]
# AVG: 400
things = undefined
measure "inline", ->
things = (x for x in array)
# AVG: 355
things = undefined
measure "Array.push", ->
things = []
for factor in array
things.push factor
# To avoid that coffee creates an anonymous array
# for the return value of this function.
return things
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment