Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Generator
(version: 3)
Comparing performance of:
ES6 generator vs Closure polyfill
Created:
9 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function* createGenerator() { var i; for (i = 0; i < 100; i++) { yield i; } } function createClosure() { var i = 0; return { next() { var result; if (i < 100) { result = { value: i, done: false }; } else { result = { value: undefined, done: true }; } i++; return result; } }; }
Tests:
ES6 generator
var gen = createGenerator(), result; while (!(result = gen.next()).done) { window.result = result.value; }
Closure polyfill
var gen = createClosure(), result; while (!(result = gen.next()).done) { window.result = result.value; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
ES6 generator
Closure polyfill
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
27 days ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.4 Safari/605.1.15
Browser/OS:
Safari 26 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
ES6 generator
920784.1 Ops/sec
Closure polyfill
917441.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested. **Benchmark Definition** The benchmark definition is a JavaScript script that creates two different types of generators: 1. **Generator**: A built-in JavaScript generator function (`createGenerator`) that yields values on each iteration. 2. **Closure polyfill**: A custom implementation of a closure using an object with a `next` method (`createClosure`). This is likely a polyfill for older browsers that don't support closures natively. The benchmark script prepares the generators by creating instances and setting up the execution loop. **Test Cases** There are two individual test cases: 1. **ES6 generator**: Tests the built-in JavaScript generator function. 2. **Closure polyfill**: Tests the custom implementation of a closure. **Options Compared** In this benchmark, we have two options being compared: 1. **Built-in JavaScript Generator**: The native `createGenerator` function, which is supported by most modern browsers. 2. **Closure Polyfill**: A custom implementation that mimics the behavior of a generator, likely for older browsers that don't support generators natively. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: **Built-in JavaScript Generator (ES6)** Pros: * Native support in most modern browsers * Efficient execution * Easy to implement Cons: * May not work in older browsers that don't support ES6 syntax * Requires compatibility layer for very old browsers (e.g., IE) **Closure Polyfill** Pros: * Works in all browsers, including very old ones * Can be optimized for specific use cases Cons: * Additional overhead due to the custom implementation * May not be as efficient as native generators * Requires more code and maintenance effort **Library/ Library Purpose** In this benchmark, there is no external library used. The `createGenerator` and `createClosure` functions are self-contained and don't rely on any external libraries. **Special JS Feature/Syntax** There's a special JavaScript feature being tested here: generators. Generators allow you to create iterators that can be paused and resumed, which is useful for cooperative multitasking and other use cases. Keep in mind that this benchmark focuses on the execution speed of generators, rather than their functional capabilities or ease of use. **Other Alternatives** If you're looking for alternatives to generators, you might consider: 1. **Callbacks**: A different approach to asynchronous programming where a function's execution is suspended until a callback function is invoked. 2. **Promises**: An object that represents the eventual completion (or failure) of an operation, allowing for easier handling of asynchronous code. 3. **Async/Await**: A syntax sugar on top of promises, making it easier to write asynchronous code that looks and feels synchronous. These alternatives have their own trade-offs and use cases, but might be worth exploring if you're interested in exploring different approaches to asynchronous programming.
Related benchmarks:
Generator with low count of items
Looping
Making Array Range
Generator and for-of
Comments
Confirm delete:
Do you really want to delete benchmark?