javascript - How can I make Sublime Text's "Improved Native for-loop" increment like a normal for-loop? -


premise

in sublime text 3, when writing javascript, typing "for" triggers insertion of 1 of 2 possible code snippets:

for (... {... (improved native for-loop)

for (var = things.length - 1; >= 0; i--) {     things[i] }; 

and

for (...) {...}

for (var = 0; < things.length; i++) {     things[i] }; 

because reasons

it has been known while reverse iteration through loops has better performance. doing means every array-based action has done backwards, , causes code become less readable. of hassle minimal performance improvements in cases.

unfortunately, "improved native for-loop" first suggestion , can result in annoying situations when accidentally chosen instead of normal, readable loop not mess typical conventions of array iteration.

questions

  • so, how 1 change behavior of sublime text not automatically add these weird loops? i.e., how can remove snippet suggestion?
  • is there way keep slight performance gains while using more traditional loop structure? how can use sublime text snippet?

purging heretical snippet

to remove "improved" for-loop snippet, create folder named "javascript" inside packages folder (preferences>browse packages), , create file named "for-()-{}-(faster).sublime-snippet" within javascript folder. inside file, place these contents:

<snippet>     <content><![cdata[$1]]></content> </snippet> 

save , restart sublime. disable built-in bass-ackwards "improved" loop snippet suggestion. @ point, normal for (...) {...} snippet active.

concerning reverse for-loop

decrementing not provide performance increases. performance increase due decreasing number of times array length has calculated: for(i=0;i<things.length;++i), things.length calculated every iteration; for(i=things.length-1; i>=0; --i), things.length calculated once. (see here.)

this mild performance increase can gained declaring new variable hold value of things.length following syntax:

for (var = 0, l = things.length; < l; ++i) {} 

creating replacement snippet

to replace default, bundled "improved" for-loop, add above syntax for-()-{}-(faster).sublime-snippet file. make sure both things placeholders selected, use ${1:things}. new snippet file contents should this:

<snippet>     <content><![cdata[for (var i=0, il=${1:things}.length; i<il; ++i) {     ${1:things}[i]; }]]>     </content>     <tabtrigger>for</tabtrigger>     <scope>source.js</scope>     <description>for (... {... custom improved for-loop</description> </snippet> 

be sure snippet properly indented. note must use tabs , not spaces indenting. save , restart sublime.

if want create similar loop language, copy contents new file , adapt content , scope language. happy coding.

edit: more advanced snippets

these snippets use, more advanced because use multiple fields (hit tab switch between fields). also, there nested loop snippet accessed typing forfor.

file name: for-()-{}-(faster).sublime-snippet

<snippet>     <content><![cdata[for (var ${1:i}=0, ${1:i}l=${2:things}.length; ${1:i}<${1:i}l; ++${1:i}) {     ${2:things}[${1:i}]; }]]>     </content>     <tabtrigger>for</tabtrigger>     <scope>source.js</scope>     <description>for (... {... custom improved for-loop</description> </snippet> 

file name: for-nested-js-(faster).sublime-snippet

<snippet>     <content><![cdata[for (var ${1:i}=0, ${1:i}l=${2:condition}; ${1:i}<${1:i}l; ++${1:i}) {      (var ${3:j}=0, ${3:j}l=${4:condition}; ${3:j}<${3:j}l; ++${3:j}) {         ${5:statement};      } }]]>     </content>     <tabtrigger>forfor</tabtrigger>     <scope>source.js</scope>     <description>for(..){for... faster nested for-loop</description> </snippet> 

Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -