Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

list.item(0) vs list[0]

document.getElementsByTagName('a').item(0)

and

document.getElementsByTagName('a')[0]

will return the same result...

Is the former faster than the latter?

like image 678
Alex Avatar asked Sep 29 '10 18:09

Alex


People also ask

What does list 0 do in Python?

python lists are 0-indexed. So the first element is 0, second is 1, so on. So if the there are n elements in a list, the last element is n-1. Remember this!

Why might you prefer to use a range over a list?

Why might you prefer to use a range over a list? Ranges do not instantiate their elements, making them more efficient in loops. Ranges are a type of list, and support all list methods as well as some special methods for range objects. Creating a range is faster than creating a list, but lists require less memory.

Are lists variables?

No. A list is an object.


1 Answers

Selfmade performance test: http://jsfiddle.net/438jh/2/

The difference seems to be negligible. The second method performs better in most case, but if you have a look on how often the loop is performed it does not really matter.

Chrome:

  1. method: ~260ms
  2. method: ~170ms
like image 151
Felix Kling Avatar answered Sep 18 '22 15:09

Felix Kling