Nessuna descrizione

bench.js 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. 'use strict'
  2. const max = 1000000
  3. const fastqueue = require('./')(worker, 1)
  4. const { promisify } = require('util')
  5. const immediate = promisify(setImmediate)
  6. const qPromise = require('./').promise(immediate, 1)
  7. const async = require('async')
  8. const neo = require('neo-async')
  9. const asyncqueue = async.queue(worker, 1)
  10. const neoqueue = neo.queue(worker, 1)
  11. function bench (func, done) {
  12. const key = max + '*' + func.name
  13. let count = -1
  14. console.time(key)
  15. end()
  16. function end () {
  17. if (++count < max) {
  18. func(end)
  19. } else {
  20. console.timeEnd(key)
  21. if (done) {
  22. done()
  23. }
  24. }
  25. }
  26. }
  27. function benchFastQ (done) {
  28. fastqueue.push(42, done)
  29. }
  30. function benchAsyncQueue (done) {
  31. asyncqueue.push(42, done)
  32. }
  33. function benchNeoQueue (done) {
  34. neoqueue.push(42, done)
  35. }
  36. function worker (arg, cb) {
  37. setImmediate(cb)
  38. }
  39. function benchSetImmediate (cb) {
  40. worker(42, cb)
  41. }
  42. function benchFastQPromise (done) {
  43. qPromise.push(42).then(function () { done() }, done)
  44. }
  45. function runBench (done) {
  46. async.eachSeries([
  47. benchSetImmediate,
  48. benchFastQ,
  49. benchNeoQueue,
  50. benchAsyncQueue,
  51. benchFastQPromise
  52. ], bench, done)
  53. }
  54. runBench(runBench)