Use length caching in JS for snippet

The previous version of this snippet referenced the object's length on
each iteration of the loop. This adds unnecessary overhead, causing the
loop to be slower. While this will not make much of a difference in many
cases, if the for loop was to be used on a large object or in a tight
loop, it would degrade performance.

Perhaps more importantly, if you were to not cache the length of a live
query, such as DOM queries, you would see significant performance
degradation. If nothing else, this adjustment to the snippet sets things
up for success in more scenarios.
This commit is contained in:
Joe Lencioni 2014-01-06 08:10:37 -08:00
parent 3b56e24062
commit bd0c576bff

View File

@ -39,8 +39,8 @@ for (var ${2:i} = ${1:Things}.length - 1; $2 >= 0; $2--) {
endsnippet endsnippet
snippet for "for (...) {...}" snippet for "for (...) {...}"
for (var ${2:i}=0; $2 < ${1:Things}.length; $2++) { for (var ${1:i} = 0, ${2:len} = ${3:Things}.length; $1 < $2; $1++) {
${3:$1[$2]}${VISUAL}$0 ${4:${3}[$1]}${VISUAL}$0
} }
endsnippet endsnippet