From bd0c576bff9a9d289aebfef86b4d59bcfa3c8096 Mon Sep 17 00:00:00 2001 From: Joe Lencioni Date: Mon, 6 Jan 2014 08:10:37 -0800 Subject: [PATCH] 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. --- UltiSnips/javascript.snippets | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/UltiSnips/javascript.snippets b/UltiSnips/javascript.snippets index bc530e9..d458750 100644 --- a/UltiSnips/javascript.snippets +++ b/UltiSnips/javascript.snippets @@ -39,8 +39,8 @@ for (var ${2:i} = ${1:Things}.length - 1; $2 >= 0; $2--) { endsnippet snippet for "for (...) {...}" -for (var ${2:i}=0; $2 < ${1:Things}.length; $2++) { - ${3:$1[$2]}${VISUAL}$0 +for (var ${1:i} = 0, ${2:len} = ${3:Things}.length; $1 < $2; $1++) { + ${4:${3}[$1]}${VISUAL}$0 } endsnippet