0

If I have a javascript array1 containing 10 000 items

what is the running time of:

 var array2=new array();
 array2.push(array1);

and what is the running time of

var object={};
object['array2']=array1;

are both O(n) ? thanks for explanation.

4
  • Might be more appropriate for programmers.stackexchange.com, where you're likely to get a more detailed, probably more scientific answer. Commented Dec 11, 2011 at 21:14
  • You should build your own test cases in jsperf.com. That's the only way to answer questions like this and then test it in the browsers you care about. Commented Dec 11, 2011 at 21:15
  • 1
    here's some interesting reading for you scottlogic.co.uk/2010/10/javascript-array-performance Commented Dec 11, 2011 at 21:17
  • 2
    @Michael: No. This is a specific question about a programming language. Much, much, much better than all the "fix my code for me plz" crap milling about; this is precisely the sort of question that should be on SO! Commented Dec 11, 2011 at 21:30

1 Answer 1

3

They should both be amortized O(1) operations.

This (obviously) depends on the browser's JS implementation, but any sane one should use an arraylist-like implentation for []s, and a hashtable-like {}. Arraylists and hashtables both have amortized O(1) insert runtime.

Sign up to request clarification or add additional context in comments.

2 Comments

is there arraylist and hashtable in javascript?
[] is more-or-less an arraylist, and {} is more-or-less a hashtable.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.