Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Generator test
(version: 0)
Comparing performance of:
With Generator vs Without Generator vs With custom generator class
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var loops = 1000; var items = Array(1000).fill(0);
Tests:
With Generator
function* mapGen(iterable, callback) { for (const item of iterable) { yield callback(item); } } let a1 = mapGen(items, (i) => i.toString()); let b1 = mapGen(a1, (i) => parseInt(i)); for (let i = 0; i < loops; ++i) { b1 = mapGen(b1, (i) => i + 1); } const reuslt = [...b1]
Without Generator
let a2 = items.map((i) => i.toString()); let b2 = a2.map((i) => parseInt(i)); for (let i = 0; i < loops; ++i) { b2 = b2.map((i) => i + 1); } const result = b2;
With custom generator class
class ArrayBuilder { _iterable; constructor(iterable) { this._iterable = iterable ?? []; } toArray() { return [...this._iterable]; } map( callback ) { return new ArrayBuilder(this._map(callback)); } *_map(callback) { let index = 0; for (const value of this._iterable) { yield callback(value, index++, this._iterable); } } } let a1 = new ArrayBuilder(items).map((i) => i.toString()); let b1 = a1.map((i) => parseInt(i)); for (let i = 0; i < loops; ++i) { b1 = b1.map((i) => i + 1); } const result = b1.toArray();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
With Generator
Without Generator
With custom generator class
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Related benchmarks:
Regular for vs forEach
Loop Test (forEach vs for)
looping with various
loop overhead
For loop vs <Array>.forEach()
Comments
Confirm delete:
Do you really want to delete benchmark?