Set Iterator vs forEach vs for..of vs spread vs Array.from

JavaScript performance comparison

Revision 4 of this test case created by Morfent

Preparation code

<script>
var set = new Set();
for (var i = 0; i < 1000; i++) {
set.add(i);
}
</script>
    

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Iterator
var result = [];
var iterator = set.values();
var iteratorResult = iterator.next();
while (!iteratorResult.done) {
result.push(iteratorResult.value);
var iteratorResult = iterator.next();
} 
pending…
forEach
var result = [];
set.forEach(function (value) {
result.push(value);
});
pending…
for..of
var result = [];
for (var value of set) {
    result.push(value);
}
pending…
spread
var result = [...set];
pending…
Array.from
var result = Array.from(set);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL.

0 Comments