By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,689 Members | 1,915 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,689 IT Pros & Developers. It's quick & easy.

Array element assignment

P: n/a
Does anyone has experience/information about the performance of the array
elements assignment?

Specifically, which is more efficient:

var myArray = new Array(a, b, c, d);

or

var myArray = new Array();
myArray[] = a;
myArray[] = b;
myArray[] = c;
myArray[] = d;
Specifically, I'm talking about multi-dimensional arrays with, all together,
several thousand elements in them.

Berislav
Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Berislav Lopac wrote:
Specifically, which is more efficient:

var myArray = new Array(a, b, c, d);

or

var myArray = new Array();
myArray[] = a;


This results into a syntax error. Do you mean:
myArray[myArray.length] = a;
?

ciao, dhgm
Jul 23 '05 #2

P: n/a
Dietmar Meier wrote:
Berislav Lopac wrote:
Specifically, which is more efficient:

var myArray = new Array(a, b, c, d);

or

var myArray = new Array();
myArray[] = a;


This results into a syntax error. Do you mean:
myArray[myArray.length] = a;
?

ciao, dhgm


Of course. I was still thinking in PHP when writing this.

Berislav
Jul 23 '05 #3

P: n/a
Berislav Lopac wrote:
Does anyone has experience/information about the performance of the array
elements assignment?

Specifically, which is more efficient:

var myArray = new Array(a, b, c, d);

or

Literals:
var myArray = [a, b, c, d];
//Not more efficient, perhaps, but less to write.
//And:

var my2dimArray = [[a,b,c,d],[1,2,3,4],["a1","b2","c3]]
var a_to_d = my2dimArray[0]
//or

var steakSauce = [[a,b,c,d],[1,2,3,4],["a1","b2","c3]][2][0]

Mick
Jul 23 '05 #4

P: n/a
"Berislav Lopac" <be************@lopsica.com> wrote in message
news:ct**********@garrison.globalnet.hr...
Does anyone has experience/information about the performance of the
array elements assignment?

Specifically, which is more efficient:

var myArray = new Array(a, b, c, d);

or

var myArray = new Array();
myArray[] = a;
myArray[] = b;
myArray[] = c;
myArray[] = d;
Specifically, I'm talking about multi-dimensional arrays with, all
together, several thousand elements in them.


I'm on a roll with this today, but, and I'll say it again: if you're
asking this sort of question, you're using the wrong tool for the job.

<url: http://blogs.msdn.com/ericlippert/ar.../18/53388.aspx />

"High performance is unimportant -- as long as the page doesn't appear
to hang, its fast enough. "

"Do not rely on "tips and tricks" for performance. People will tell you
"declared variables are faster than undeclared variables" and "modulus
is slower than bit shift" and all kinds of nonsense. Ignore them.
That's like mowing your lawn by going after random blades of grass with
nail scissors. You need to find the WORST thing, and fix it first.
That means measuring. Get some tools -- Visual Studio Analyzer can do
some limited script profiling, as can the Numega script profiler, but
even just putting some logging into the code that dumps out millisecond
timings is a good way to start. Once you know what the slowest thing
is, you can concentrate on modularizing and fixing it. "

<script type="text/javascript">
var t = (new Date()).getTime();
for (var count = 0; count < 1000; ++count)
{
var a = new Array(10);
var ii = a.length;
while (ii-- > 0)
{
a[ii] = new Array(10);
var jj = a[ii].length;
while (jj-- > 0)
{
a[ii][jj] = 9e9;
}
}
}
document.write(((new Date()).getTime() - t) + '<br>');
var t = (new Date()).getTime();
for (var count = 0; count < 1000; ++count)
{
var a = new Array(
new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9)
);
}
document.write(((new Date()).getTime() - t) + '<br>');
</script>

Creating 1000 - 10 x 10 arrays takes less than a second on my machine
either way. What difference does it make if one takes 1/4 of a second
and the other takes 1/2 second. Both are faster than the human being
looking at the Web page. Write mantainable code.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #5

P: n/a
JRS: In article <ct**********@garrison.globalnet.hr>, dated Thu, 27 Jan
2005 14:57:21, seen in news:comp.lang.javascript, Berislav Lopac
<be************@lopsica.com> posted :
Does anyone has experience/information about the performance of the array
elements assignment?

Specifically, which is more efficient: ...


You can measure the execution time in the browser(s) that you use; if
the difference(s) are not significant in comparison for the time taken
for everything, then they don't matter for you and probably not for
anyone else.

For example, I've used

function Timer() { var J, K, M=30, N=(50000/M)|0, D0, D1, D2, D3
D0 = new Date()
K = N ; while (--K) { J = M ; while (--J) { } }
D1 = new Date()
K = N ; while (--K) { J = M ; while (--J) lz(J) }
D2 = new Date()
K = N ; while (--K) { J = M ; while (--J) LZ(J) }
D3 = new Date()
alert('Nul ' + (D1-D0) + ', lz ' + (D2-D1) + ', LZ ' + (D3-D2)) }

to compare

function LZ(x) { return (x<0||x>=10?"":"0") + x }

function lz(x) { var t = String(x)
return t.length==1 ? "0"+t : t }

with the result that LZ seems about 25% better.

Those with faster computers may need to increase the big number.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #6

P: n/a
Grant Wagner wrote:
"Berislav Lopac" <be************@lopsica.com> wrote in message
news:ct**********@garrison.globalnet.hr...
Does anyone has experience/information about the performance of the
array elements assignment?

Specifically, which is more efficient:

var myArray = new Array(a, b, c, d);

or

var myArray = new Array();
myArray[] = a;
myArray[] = b;
myArray[] = c;
myArray[] = d;
Specifically, I'm talking about multi-dimensional arrays with, all
together, several thousand elements in them.

I'm on a roll with this today, but, and I'll say it again: if you're
asking this sort of question, you're using the wrong tool for the job.

<url: http://blogs.msdn.com/ericlippert/ar.../18/53388.aspx />

"High performance is unimportant -- as long as the page doesn't appear
to hang, its fast enough. "

"Do not rely on "tips and tricks" for performance. People will tell you
"declared variables are faster than undeclared variables" and "modulus
is slower than bit shift" and all kinds of nonsense. Ignore them.
That's like mowing your lawn by going after random blades of grass with
nail scissors. You need to find the WORST thing, and fix it first.
That means measuring. Get some tools -- Visual Studio Analyzer can do
some limited script profiling, as can the Numega script profiler, but
even just putting some logging into the code that dumps out millisecond
timings is a good way to start. Once you know what the slowest thing
is, you can concentrate on modularizing and fixing it. "

<script type="text/javascript">
var t = (new Date()).getTime();
for (var count = 0; count < 1000; ++count)
{
var a = new Array(10);
var ii = a.length;
while (ii-- > 0)
{
a[ii] = new Array(10);
var jj = a[ii].length;
while (jj-- > 0)
{
a[ii][jj] = 9e9;
}
}
}
document.write(((new Date()).getTime() - t) + '<br>');
var t = (new Date()).getTime();
for (var count = 0; count < 1000; ++count)
{
var a = new Array(
new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9)
);
}
document.write(((new Date()).getTime() - t) + '<br>');
</script>

Creating 1000 - 10 x 10 arrays takes less than a second on my machine
either way. What difference does it make if one takes 1/4 of a second
and the other takes 1/2 second. Both are faster than the human being
looking at the Web page. Write mantainable code.


I've got about six months experience with js and agree for the most part
with your arguements - however I would recommend

var myArray = new Array(a, b, c, d);

instead of

var myArray = new Array();
myArray[] = a;
myArray[] = b;
myArray[] = c;
myArray[] = d;
The OP mentions they will have several thousands of elements - If this
is true, using the former method would at very least mean less data over
the wire. Small gains - but as the saying goes - look after the pennies
and the pounds will look after themselves...

randelld
Jul 23 '05 #7

P: n/a
Grant Wagner wrote:
"High performance is unimportant -- as long as the page doesn't appear
to hang, its fast enough. "


Actually, it does, on some machines (I didn't get the specs, but my guess
they are not top of the line).

Berislav
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.