<!--your preparation HTML code goes here-->
const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
arr.filter(n => n % 2 === 0).map(n => ({ result: n })).forEach(n => console.log("n = ", n));
const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
const iterator = Iterator.from(arr);
iterator.filter(n => n % 2 === 0).map(n => ({ result: n })).forEach(n => console.log("n = ", n));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Arrays | |
Iterator |
Test name | Executions per second |
---|---|
Arrays | 1992.2 Ops/sec |
Iterator | 2687.2 Ops/sec |
The benchmark titled "Iterators vs Arrays" compares the performance between two different programming constructs in JavaScript: traditional arrays and an iterator-based approach using a hypothetical Iterator.from
method. Each test case evaluates how efficiently each construct can handle a common operation: filtering even numbers from an array and mapping them to a new object structure.
Arrays
const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
arr.filter(n => n % 2 === 0).map(n => ({ result: n })).forEach(n => console.log("n = ", n));
result
, and finally logs each result to the console.filter
, map
, and forEach
are well-optimized in most JavaScript engines.Iterator
const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
const iterator = Iterator.from(arr);
iterator.filter(n => n % 2 === 0).map(n => ({ result: n })).forEach(n => console.log("n = ", n));
Iterator.from
, and the same filter, map, and log operations are performed through the iterator.Iterator.from
method isn't standard in JavaScript, which may make it less familiar to many developers. Depending on the implementation, it might be less optimized compared to built-in array methods.The iterator approach outperformed the traditional array approach in this benchmark, suggesting that for this specific use case, the iterator can handle the operations more efficiently.
Use Cases: Choosing between arrays and iterators depends on the context of the application. For data that doesn't fit into memory (like streams of data), iterators are preferred. For scenarios with smaller datasets where simplicity and readability are more critical, arrays may be more suitable.
Library: The benchmark utilizes a non-standard Iterator
class. While this may not be part of the JavaScript language specification currently, there are libraries like async iterator
or rxjs
that can provide similar functionality with different performance characteristics and additional features.
There are several alternatives to the approaches tested:
Higher-order Functions: Using traditional methods like for
loops for manual iteration may yield better performance in some contexts, especially if benchmarks can be optimized by eliminating function calls in favor of raw iteration.
Functional Libraries: Libraries like Lodash or Ramda provide functions that can sometimes offer better performance or additional functionality, especially for complex data transformations or when working with immutable data.
WebAssembly: For compute-heavy operations often found in benchmarks, utilizing WebAssembly could also provide significant speed improvements, especially for operations that require extensive mathematical computations or processing large datasets.
In summary, the benchmark provides a valuable comparison between JavaScript arrays and iterator-based approaches, showcasing the trade-offs in performance and usage considerations that software engineers must account for in their code.