473,473 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Array element assignment

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
7 1555
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
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
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kaptain524 | last post by:
Hello, I am using PHP 5.0.4 with Apache 2, on WinXP Pro. This behavior appears to be fundamental however, and should not be affected by platform. It would seem that there is some kind of bug...
8
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
6
by: Irrwahn Grausewitz | last post by:
Hey, y'all. While doing some pointer experiments I encountered a problem. I know that I know the answer already, but trying to remember I just screwed up my mind. I wonder if someone would be...
20
by: Petter Reinholdtsen | last post by:
Is the code fragment 'char a = ("a");' valid ANSI C? The problematic part is '("a")'. I am sure 'char a = "a";' is valid ANSI C, but I am more unsure if it is allowed to place () around the...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
15
by: Paminu | last post by:
Still having a few problems with malloc and pointers. I have made a struct. Now I would like to make a pointer an array with 4 pointers to this struct. #include <stdlib.h> #include <stdio.h>...
3
by: Eric Lilja | last post by:
Assignment: Create a 3x4 2-dimensional array of integers. Populate each element using some non-random scheme so that no two elemens contain the same value. Then create two functions for printing...
57
by: buuuuuum | last post by:
why array can't be assigned, like structs?
9
by: Lionel B | last post by:
Hi, I am trying to get my head around this: int main() { typedef int array1; array1 x,y; y = x; // error: invalid array assignment
4
by: drktmplr11 | last post by:
Hi, this is my first post here at the forums, and I am looking for assistance with what looks to be a syntax error within my code. I am attending FIU, and looking to broaden my understanding of...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.