Skip to content

Instantly share code, notes, and snippets.

@pqwy
Created October 23, 2012 00:11
Show Gist options
  • Save pqwy/3935741 to your computer and use it in GitHub Desktop.
Save pqwy/3935741 to your computer and use it in GitHub Desktop.
v8: closure vs. lambda-lifting
# This is LiveScript, really, but GH can't colour it -- yet.
time = (n, target) ->
t0 = (new Date).getTime!
for i from 1 to n then r = target!
t = (new Date).getTime! - t0
ta = t / n
console.log "[time] #t ms (avg #ta ms)"
r
make-adder = (x) -> (n) -> (x + n)
[ a, b ] = [ 1324, 1343 ]
console.log "closures"
let adder = make-adder a
time 1000, !-> for i from 0 to 100000 then adder b
console.log "arguments"
let adder = ((x, n) -> (x + n))
time 1000, !-> for i from 0 to 100000 then adder a, b
# closures
# [time] 491 ms (avg 0.491 ms)
# arguments
# [time] 480 ms (avg 0.48 ms)
#
# The slowdown is insignificant in comparison to, say, using varargs, but still exists.
# http://mrale.ph/blog/2012/09/23/grokking-v8-closures-for-fun.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment