DEV Community

Discussion on: Avoid slow Javascript code

Collapse
 
hadaward profile image
Hadaward

There is another way to do the for loop:

for(var i=0, k=arr.length; i < k; i++) {}

The first division to define "i" is always called once and you can define as many variables as you like, this is even better because instead of creating a global variable you can use "let" and make local variables (it has better performance and keeps the code organized)

Collapse
 
metalmikester profile image
Michel Renaud

The examples in the article should use let instead of var anyway. Nice one with the "k" in your example.

Collapse
 
cleveroscar profile image
Oscar Ortiz

You’re are correct, I still need to revise it and edit it! I really appreciate the feedback!

I know “let” is the new way to declare variables and “var” was pretty much the old school way.

Thread Thread
 
metalmikester profile image
Michel Renaud

It's not so much "new" vs. "old" school as they are not quite the same. "var" is basically available everywhere whereas "let" is limited to a scope.

Collapse
 
amansaxena001 profile image
Amansaxena001

Block scope is best either with let or IIFE