W3C home > Mailing lists > Public > www-dom@w3.org > October to December 2011

toArray() method for array-like DOM list objects

From: Marat Tanalin | tanalin.com <mtanalin@yandex.ru>
Date: Thu, 22 Dec 2011 22:19:00 +0400
To: www-dom@w3.org
Message-Id: <80181324577940@web83.yandex.ru>
It would be nice to add toArray() method to all array-like DOM list objects, e.g. DOMNodeList and DOMTokenList.

DOMNodeList is returned, for example, by document.getElementsByTagName() and element.querySelectorAll() methods.
DOMTokenList is returned, for example, by element.classList property.

Converting DOM list to Array is often used in real-world JavaScript applications.

Currently, two ways for this purpose are used most widely:

1. Array.prototype.slice.call(domList, 0);
2. full manual iterating trough DOM list and adding items one by one to an array.

First way seems to be a hack, second one is just slow as any pure-script iterating is.

Introducing _native_ toArray() method would be multiple times faster than pure-script solutions and thus would hugely speedup the whole Web.

Example for additional clarity:

assuming a document with two DIV elements (<div></div>), following call:

would return Array object with two DIV elements as Array items.

Received on Thursday, 22 December 2011 18:19:35 UTC

This archive was generated by hypermail 2.4.0 : Friday, 17 January 2020 22:36:59 UTC