Skip to content

Instantly share code, notes, and snippets.

@wwalser
Created May 17, 2012 11:58

Revisions

  1. wwalser revised this gist May 17, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@
    * good but quickly diminishing. I found that if don't let Chrome use MessageChannel (the
    * implementation it defaults to) it falls back to what Firefox uses and performance stabilizes.
    *
    * Below is test code you can run in developer tools and jsPerf test cases can be found at:
    * This Gist is test code you can run in developer tools and jsPerf test cases can be found at:
    * (message channel, slow) http://jsperf.com/setimmediate-with-message-channel
    * (no message channel, fast) http://jsperf.com/setimmediate-without-message-channel
    */
  2. wwalser revised this gist May 17, 2012. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,7 @@
    /**
    * Minified https://github.com/NobleJS/setImmediate
    */
    (function(a,h){function i(){if(!a.postMessage||a.importScripts)return!1;var b=!0,f=a.onmessage;a.onmessage=function(){b=!1};a.postMessage("","*");a.onmessage=f;return b}function j(b){b.setImmediate=a.msSetImmediate;b.clearImmediate=a.msClearImmediate}function k(b){b.setImmediate=function(){var b=c.addFromSetImmediateArguments(arguments),d=new a.MessageChannel;d.port1.onmessage=function(){c.runIfPresent(b)};d.port2.postMessage(null);return b}}function l(b){function f(b){b.source===a&&("string"===typeof b.data&& b.data.substring(0,d.length)===d)&&(b=b.data.substring(d.length),c.runIfPresent(b))}var d="com.bn.NobleJS.setImmediate"+Math.random();a.addEventListener?a.addEventListener("message",f,!1):a.attachEvent("onmessage",f);b.setImmediate=function(){var b=c.addFromSetImmediateArguments(arguments);a.postMessage(d+b,"*");return b}}function m(b){b.setImmediate=function(){var b=c.addFromSetImmediateArguments(arguments),a=document.createElement("script");a.onreadystatechange=function(){c.runIfPresent(b);a.onreadystatechange= null;a.parentNode.removeChild(a);a=null};document.documentElement.appendChild(a);return b}}function n(b){b.setImmediate=function(){var b=c.addFromSetImmediateArguments(arguments);a.setTimeout(function(){c.runIfPresent(b)},0);return b}}var c=function(){function b(b,a){this.handler=b;this.args=Array.prototype.slice.call(a)}b.prototype.run=function(){"function"===typeof this.handler?this.handler.apply(h,this.args):eval(""+this.handler)};var e=1,d={},g=!1;return{addFromSetImmediateArguments:function(a){var c= a[0],a=Array.prototype.slice.call(a,1),c=new b(c,a),a=e++;d[a]=c;return a},runIfPresent:function(b){if(g)a.setTimeout(function(){c.runIfPresent(b)},0);else{var e=d[b];if(e){g=!0;try{e.run()}finally{delete d[b],g=!1}}}},remove:function(a){delete d[a]}}}();if(!a.setImmediate){var e="function"===typeof Object.getPrototypeOf&&"setTimeout"in Object.getPrototypeOf(a)?Object.getPrototypeOf(a):a;a.msSetImmediate&&a.msClearImmediate?j(e):(a.MessageChannel?k(e):i()?l(e):"document"in a&&"onreadystatechange"in a.document.createElement("script")?m(e):n(e),e.clearImmediate=c.remove)}})(this);
    /**
    * Did some performance tests using the absolutely awesome cross browser setImmediate
    * implementation at https://github.com/NobleJS/setImmediate.
  3. wwalser revised this gist May 17, 2012. 1 changed file with 12 additions and 2 deletions.
    14 changes: 12 additions & 2 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,15 @@
    /**
    * https://github.com/NobleJS/setImmediate
    * Did some performance tests using the absolutely awesome cross browser setImmediate
    * implementation at https://github.com/NobleJS/setImmediate.
    *
    * I noticed that in Firefox performance of setImmediate remained steady and consistently
    * outperformed the setTimeout(fun, 0) trick. Chrome was a different story with performance initially
    * good but quickly diminishing. I found that if don't let Chrome use MessageChannel (the
    * implementation it defaults to) it falls back to what Firefox uses and performance stabilizes.
    *
    * Below is test code you can run in developer tools and jsPerf test cases can be found at:
    * (message channel, slow) http://jsperf.com/setimmediate-with-message-channel
    * (no message channel, fast) http://jsperf.com/setimmediate-without-message-channel
    */
    function createContinuation(){
    var recurseMethod = Array.prototype.shift.call(arguments),
    @@ -24,4 +34,4 @@ setInterval(function(){
    console.log('immediate: ' + immediateCount());
    console.log('delta: ' + (immediateCount() - timeoutCount()));
    console.log('percentage: ' + (timeoutCount()/immediateCount())*100);
    }, 1000);
    }, 5000);
  4. wwalser created this gist May 17, 2012.
    27 changes: 27 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    /**
    * https://github.com/NobleJS/setImmediate
    */
    function createContinuation(){
    var recurseMethod = Array.prototype.shift.call(arguments),
    args = arguments,
    count = 0,
    continuation = function(){
    count++;
    recurseMethod.apply(window, args);
    }

    Array.prototype.unshift.call(args, continuation);
    continuation();

    return function(){
    return count;
    }
    }
    var timeoutCount = createContinuation(setTimeout, 0);
    var immediateCount = createContinuation(setImmediate);
    setInterval(function(){
    console.log('timeout: ' + timeoutCount());
    console.log('immediate: ' + immediateCount());
    console.log('delta: ' + (immediateCount() - timeoutCount()));
    console.log('percentage: ' + (timeoutCount()/immediateCount())*100);
    }, 1000);