You are here: Reference > JavaScript > core > objects > Enumerator

Enumerator object

Browser support:
Provides a faster iterating mechanism for collections.
The Enumerator object is useful if you want iterate through a large collection.
Iterating through a large collection with the Enumerator object is faster than any other way in JavaScript. Example 2 shows a speed test.
The Enumerator object allows using a walk-through mechanism to iterate through the items.
There is a pointer, which designates the currently selected element and can be used to navigate within the Enumerator object. In a newly created Enumerator object, the currently selected item is the first item of the collection.

Syntax:

Creating a new Enumerator object:
var myEnum = new Enumerator ([collection]);


collection Optional. Specifies the collection for enumeration.

Members:

The Enumerator object inherits from the Enumerator.prototype and Function.prototype objects. The following lists only contain the members of the Enumerator and Enumerator.prototype objects.

Properties:

Property Support Description
prototype
Returns a reference to the Enumerator.prototype object. The Enumerator.prototype object allows adding properties and methods to the Enumerator object that can be used with instances of the Enumerator object, like any predefined property or method. The prototype property is static, it cannot be accessed from an instance of the Enumerator object, only Enumerator.prototype is allowed.

Methods:

Method Support Description
atEnd ( )*
Returns a Boolean value that indicates whether the pointer of the Enumerator is at the end of the collection, or not.
item ( )*
Returns the currently selected item (where the pointer is placed) from the collection.
moveFirst ( )*
Moves the pointer of the Enumerator to the first item of the collection.
moveNext ( )*
Moves the pointer of the Enumerator to the next item in the collection.

(*) - The method is inherited from the Enumerator.prototype.

Examples:

Example 1:

This example shows how to iterate through the attributes collection with the Enumerator object:
<head>
    <style>
        #info {
            background-color:#e0a0a0;
            width:300px;
            height:300px;
            overflow:auto;          
        }
    </style>
    <script type="text/javascript">
        function GetAttrs () {
            var attrNames = "";
            if (typeof (Enumerator) == "undefined") {
                attrNames = "Your browser does not support the Enumerator object!";
            }
            else {
                var attrs = new Enumerator (document.body.attributes);
                while (!attrs.atEnd ())    {
                    var item = attrs.item ();
                    attrNames += item.name + "<br />";
                    attrs.moveNext ();
                }
            }
            return attrNames;
        }

        function Init () {
            var attrNames = GetAttrs ();
            var info = document.getElementById ("info");
            info.innerHTML = attrNames;
        }
    </script>
</head>
<body onload="Init ()">
    <div id="info"></div>
</body>
Did you find this example helpful? yes no

Example 2:

This example shows a speed test for iterating through a collection with an Enumerator object:
<head>
    <style>
        #info {
            background-color:#e0a0a0;
            width:300px;
            height:300px;
            overflow:auto;          
        }
    </style>
    <script type="text/javascript">
        function SpeedTest () {
            var speed = "";
            if (typeof (Enumerator) == "undefined") {
                speed = "Your browser does not support the Enumerator object!";
            }
            else {
                var start = new Date ();
                var attrs = document.body.attributes;
                for (var repeat = 0; repeat < 100; repeat++) {
                    for (var i = 0; i < attrs.length; i++) {
                        var item = attrs.item (i);
                    }
                }
                var end = new Date ();
                speed += "Collection: " + (end.valueOf () - start.valueOf ()) + " ms<br />";
                
                start = new Date ();
                var attrEnum = new Enumerator (document.body.attributes);
                for (var repeat = 0; repeat < 100; repeat++) {
                    attrEnum.moveFirst ();
                    while (!attrEnum.atEnd ()) {
                        var item = attrEnum.item ();
                        attrEnum.moveNext ();
                    }
                }
                end = new Date ();
                speed += "Enumerator: " + (end.valueOf () - start.valueOf ()) + " ms<br />";
            }
            
            return speed;
        }

        function Init () {
            var speed = SpeedTest ();
            var info = document.getElementById ("info");
            info.innerHTML = speed;
        }
    </script>
</head>
<body onload="Init ()">
    Iterating through the entire attributes collection of the body takes:
    <div id="info" ></div>
</body>
Did you find this example helpful? yes no

External links:

User Contributed Comments

Post Content

Post Content