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

Edit on GitHub

Convert a Set into an Array

Setup

var _set = new Set();
for (var i = 0; i < 50000; i++) {
_set.add(i*2);
}

Tests

  • Iterator

    var result = [];
    var iterator = _set.values();
    var iteratorResult = iterator.next();
    while (!iteratorResult.done) {
    result.push(iteratorResult.value);
    var iteratorResult = iterator.next();
    }
  • forEach

    var result = [];
    _set.forEach(function (value) {
    result.push(value);
    });
  • for..of

    var result = [];
    for (var value of _set) {
        result.push(value);
    }
  • spread

    var result = [..._set];
  • Array.from

    var result = Array.from(_set);