benchmark.js icon indicating copy to clipboard operation
benchmark.js copied to clipboard

quick async example in readme

Open tj opened this issue 12 years ago • 6 comments

to get started would be useful. Can you pass a callback to Suite#add()'s function or how does the async support work? The readme example has async: true but the code isn't async

tj avatar Aug 09 '13 22:08 tj

Ok will do. The async option is executing the tests async to give the UI a chance to update. What you want is the defer option to use the deferred object and its deferred.resolve(). See docs/#Benchmark.

jdalton avatar Aug 10 '13 07:08 jdalton

ah! I see cool

tj avatar Aug 10 '13 07:08 tj

The async option is executing the tests async to give the UI a chance to update.

@jdalton does it mean that the async: true/false can be ignored if used in node? because there is no UI in node.

luckydrq avatar Oct 23 '14 15:10 luckydrq

does it mean that the async: true/false can be ignored if used in node? because there is no UI in node.

It may still lock up the terminal while it crunches through them so it's up to you.

jdalton avatar Oct 23 '14 17:10 jdalton

ok, thank you!

luckydrq avatar Oct 24 '14 01:10 luckydrq

So when you specify 'defer': true as option the test function you provide gets a deferred object with a resolve function? (This wasn't really obvious for me looking through the docs).

So the following would work?

// Using deferred test
var bench = new Benchmark('foo', function(deferred) {
    // call resolve() when the deferred test is finished
    deferred.resolve();
  }, {
  // a flag to indicate the benchmark is deferred
  'defer': true,
});
// Using deferred test
var bench = new Benchmark('foo', {
  // a flag to indicate the benchmark is deferred
  'defer': true,

  // benchmark test function
  'fn': function(deferred) {
    // call resolve() when the deferred test is finished
    deferred.resolve();
  }
});

I would also like a deferred example in the readme, maybe something like:

... 
// add deferred test
suite.add('Deferred test', function(deferred) {
    // call resolve() when the deferred test is finished
    deferred.resolve();
}, {'defer': true})
...

peteruithoven avatar May 29 '15 10:05 peteruithoven