@jaffathecake Is there a reason code executed on the global scope is slower than the same code in a function called from the global scope. Same behaviour in other browsers bar Safari?
-
-
Replying to @jaffathecake @robmcm
It might also be an issue with the benchmark. How did you measure exactly? I don’t know why fn() would be slower, run-time wise, than (function() { fn() }()) — if anything I’d expect the opposite. +
@bmeurer2 replies 0 retweets 3 likes -
Well `var x` on global scope is a property of the global object whereas `var x` in a function is a stack local (or worst case context local). Accessing object properties is obviously slower than accessing stack slots.
1 reply 0 retweets 12 likes -
*goes and wraps entire codebase in IIFE*
3 replies 0 retweets 9 likes -
This is a well known technique for scripts to avoid introducing globals, right?
1 reply 0 retweets 0 likes
I agree it is. It then later became a speed hack™ to help the pre-parser (which tend to skip inner functions) https://github.com/nolanlawson/optimize-js …
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.
JavaScript, HTML, CSS, HTTP, performance, security, Bash, Unicode, i18n, macOS.