Last active
January 2, 2016 03:29
-
-
Save qubyte/8243729 to your computer and use it in GitHub Desktop.
Demonstrates the avoidance of a closure for the trivial task of getting a time difference from invocation using a generator.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dt = (function () { | |
var start = Date.now(); | |
return function () { | |
return Date.now() - start; | |
}; | |
})(); | |
// Some time later... | |
console.log(dt()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dt = (() => { | |
let gen = (function* () { | |
let start = Date.now(); | |
while (true) { | |
yield Date.now() - start; | |
} | |
})(); | |
gen.next(); // Start the clock... | |
return gen; | |
})(); | |
// Some time later... | |
console.log(dt.next().value); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment