What's the most efficient way to create this simple array dynamically.
var arr = [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"];
Let's say we can get the number 10 from a variable
var mynumber = 10;
What's the most efficient way to create this simple array dynamically.
var arr = [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"];
Let's say we can get the number 10 from a variable
var mynumber = 10;
I hope you have to get last element from array variable so my solution
This seems to be faster in Chrome, according to JSPerf, but please note that it is all very browser dependant.
There's 4 things you can change about this snippet:
for
orwhile
.push
or direct access by index.toString
.In each and every browser total speed would be combination of how much better each option for each item in this list performs in that particular browser.
TL;DR: it is probably not good idea to try to micro-optimize this particular piece.
Here is how I would do it:
My answer is pretty much the same of everyone, but note that I did something different:
So I created the array with
new Array(mynumber);
I would do as follows;