timeout via audio context

Benchmark created by Andrew on


Description

This is a workaround for the issue of window.setTimeout, that it is being paused in iOS Safari during touch events

Setup

window.AudioContext = window.AudioContext || window.webkitAudioContext;
  var context = new AudioContext();

  Benchmark.prototype.setup = function() {
  function sum(){return 1+1};
    AudioTimeout = {
      set: function(callback, length) {
        if (length <= 0) {
          length = 1;
        }
        var source = context.createBufferSource();
        source.buffer = context.createBuffer(1, 32000 * (length / 1000), 32000);
        source.connect(context.destination);
        source.onended = callback;
        if (!source.stop) {
          source.stop = source.noteOff;
        }
        if (!source.start) {
          source.start = source.noteOn;
        }
        source.start(0);
        return source;
      },
      clear: function(source){
        source.stop(0);
      }
  };

  };

Test runner

Ready to run.

Testing in
TestOps/sec
audio timeout
AudioTimeout.set(sum, 1);
ready
setTimeout
setTimeout(sum, 1);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.