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
// See http://www.bluejava.com/4NS/Speed-up-your-Websites-with-a-Faster-setTimeout-using-soon | |
// This is a very fast "asynchronous" flow control - i.e. it yields the thread and executes later, | |
// but not much later. It is far faster and lighter than using setTimeout(fn,0) for yielding threads. | |
// Its also faster than other setImmediate shims, as it uses Mutation Observer and "mainlines" successive | |
// calls internally. | |
// WARNING: This does not yield to the browser UI loop, so by using this repeatedly | |
// you can starve the UI and be unresponsive to the user. | |
// Note: For an even faster version, see https://gist.github.com/bluejava/b3eb39911da03a740727 | |
var soon = (function() { | |
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
// See http://www.bluejava.com/4NS/Speed-up-your-Websites-with-a-Faster-setTimeout-using-soon | |
// This is a very fast "asynchronous" flow control - i.e. it yields the thread and executes later, | |
// but not much later. It is far faster and lighter than using setTimeout(fn,0) for yielding threads. | |
// Its also faster than other setImmediate shims, as it uses Mutation Observer and "mainlines" successive | |
// calls internally. | |
// WARNING: This does not yield to the browser UI loop, so by using this repeatedly | |
// you can starve the UI and be unresponsive to the user. | |
// This is an even FASTER version of https://gist.github.com/bluejava/9b9542d1da2a164d0456 that gives up | |
// passing context and arguments, in exchange for a 25x speed increase. (Use anon function to pass context/args) | |
var soon = (function() { |
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
/** | |
* This function uses the Page Visibility API to puase/restart the timeout such that | |
* the time you specify equates to "visibility time". | |
* Note: There is no clearTimeout capability - though it would not be hard to extend this to | |
* provide cancel-ability | |
* | |
* Usage: | |
* setVisiTimeout(function, ms, arg1, arg2, ... ); | |
* | |
* LICENSE: Unlicense <http://unlicense.org/> / CC0 |
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
// Demonstration of a Markov Text Generator to create a Sarah Palin endorsement for Donald Trump. | |
// See blog post at http://www.bluejava.com/4P9/Sarah-Palin-vs-12-lines-of-JavaScript | |
var text = "“Thank you so much. It’s so great to be here in Iowa. We’re here just thawing out. Todd and I and a couple of our friends here from Alaska, lending our support for the next president of our great United States of America, Donald J. Trump. “Mr. Trump, you’re right, look back there in the press box. Heads are spinning, media heads are spinning. This is going to be so much fun. “Are you ready to make America great again? We all have a part in this. We all have a responsibility. Looking around at all of you, you hardworking Iowa families. You farm families, and teachers, and teamsters, and cops, and cooks. You rockin’ rollers. And holy rollers! All of you who work so hard. You full-time moms. You with the hands that rock the cradle. You all make the world go round, and now our cause is one. “When asked why I would jump |
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 batchSize = 1000, | |
doBluebirdAll = true, | |
doZousanAll = true, | |
doNativeAll = true, | |
doBluebirdSeries = true, | |
doZousanSeries = true, | |
doNativeSeries = true | |
console.log(" - Batch Size: " + batchSize) |
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
Promise.eval = function(ar) | |
{ | |
// We accept either an array or items specified as individual arguments... | |
// ...but we convert the series into an array either way | |
if(!Array.isArray(ar)) | |
ar = Array.prototype.slice.call(arguments) | |
var items = { } // map names to items | |
// This forEach is where it all happens - we evaluate each item within the eval array. |
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
Zousan.evaluate( | |
{ name: "username", value: "glenn" }, | |
{ name: "score", value: 45 } | |
).then(function(ob) { | |
console.log("Hello " + ob.username + ". Your score is " + ob.score) | |
}) |
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
function getScore() { return 45 } | |
function double(x) { return x * 2 } | |
Zousan.evaluate( | |
{ name: "username", value: "glenn" }, | |
{ name: "score", value: getScore }, | |
{ name: "newScore", value: double, deps: [ "score" ] } | |
).then(function(ob) { | |
console.log("Hello " + ob.username + ". Your new score is " + ob.newScore) | |
}) |
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
function getUser(name) { /* returns Promise of user object */ } | |
function getFavorites(user) { /* returns Promise of favorites array */ } | |
Zousan.evaluate( | |
{ name: "username", value: "glenn" }, | |
{ name: "user", value: getUser, deps: [ "username" ] }, | |
{ name: "favs", value: getFavorites, deps: [ "user" ] } | |
).then(function(ob) { | |
renderUserFavs(ob.user, ob.favs) | |
}) |
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
Zousan.evaluate( | |
{ name: "username", value: "glenn" }, | |
{ name: "user", value: getUser, deps: [ "username" ] }, | |
{ name: "favs", value: getFavorites, deps: [ "user" ] }, | |
{ value: renderUser, deps: [ "user" ] }, | |
{ value: renderUserFavs, deps: [ "user", "favs" ] } | |
) |
OlderNewer