BackburnerJS / backburner.js

A rewrite of the Ember.js run loop as a generic microlibrary
MIT License
392 stars 80 forks source link

avoid creating runloop for debounce and throttle #319

Closed bekzod closed 6 years ago

bekzod commented 6 years ago

coalesces debounce, throttle, later callbacks together so they run within one run loop

rwjblue commented 6 years ago

The PR title says WIP, can you update the description with a checklist of items that need to be done still (it seems mostly complete to me)?

bekzod commented 6 years ago

currently this changes performing badly in benchmarks both debounce and throttle

stefanpenner commented 6 years ago

currently this changes performing badly in benchmarks both debounce and throttle

I am surprised by this.

bekzod commented 6 years ago

with more realistic benchmarks #323 before:

 Debounce - function ................................................................ 247,947.95 op/s
 Debounce & Cancel - function, target ............................................... 208,892.36 op/s
 Throttle - function .............................................................. 3,916,413.49 op/s
 Throttle & Cancel - function, target ............................................... 695,790.00 op/s

after

  Debounce - function ................................................................ 166,956.34 op/s
  Debounce & Cancel - function, target ................................................ 93,113.25 op/s
  Throttle - function .............................................................. 5,556,379.79 op/s
  Throttle & Cancel - function, target ................................................ 52,232.04 op/s
bekzod commented 6 years ago

This is ready for review, there is one difference though with previous functionality, .later, .debouce and .throttle now overlap with each other, for example:

let func = ()=> {}

bb.later(func, 500);
bb.debounce(func, 200); // same with bb.throttle
// func will be called once in 200

// this could be avoided with 

bb.later(()=> func(), 500);
bb.debounce(func, 200); // same with bb.throttle

BEFORE

Debounce - function ................................................................ 216,127.78 op/s
  Debounce & Cancel - function, target ............................................... 210,626.71 op/s
  Later & Cancel - function .......................................................... 298,704.45 op/s
  Later & Cancel - function, target .................................................. 274,266.20 op/s
  Schedule & Cancel - function ..................................................... 3,868,434.81 op/s
  Schedule & Cancel - target, function ............................................. 1,577,840.51 op/s
  Schedule & Cancel - target, string method name ................................... 1,534,608.39 op/s
  Schedule & Cancel - target, string method name, 1 argument ....................... 1,504,238.03 op/s
  Schedule & Cancel - target, string method name, 2 arguments ...................... 1,443,822.69 op/s
  Schedule & Cancel - prescheduled, same queue - target, string method name .......... 831,927.89 op/s
  Schedule & Cancel - prescheduled, separate queue - target, string method name .... 1,514,822.94 op/s
  Schedule & Flush - function ........................................................ 432,442.41 op/s
  Schedule & Flush - target, function ................................................ 419,131.55 op/s
  Schedule & Flush - target, string method name ...................................... 409,880.51 op/s
  Schedule & Flush - target, string method name, 1 argument .......................... 390,206.65 op/s
  Schedule & Flush - target, string method name, 2 arguments ......................... 392,836.33 op/s
  DEBUG - Schedule & Flush - function ................................................ 109,377.44 op/s
  DEBUG - Schedule & Flush - target, function ........................................ 109,197.47 op/s
  DEBUG - Schedule & Flush - target, string method name .............................. 110,225.60 op/s
  DEBUG - Schedule & Flush - target, string method name, 1 argument .................. 108,036.29 op/s
  DEBUG - Schedule & Flush - target, string method name, 2 arguments ................. 106,298.38 op/s
  Throttle - function .............................................................. 4,734,094.44 op/s
  Throttle & Cancel - function, target ............................................. 1,745,480.28 op/s
fastest: Throttle - function

AFTER

  Debounce - function ................................................................ 538,407.09 op/s
  Debounce & Cancel - function, target ............................................... 363,653.61 op/s
  Later & Cancel - function ........................................................ 1,048,227.07 op/s
  Later & Cancel - function, target ................................................ 1,060,948.23 op/s
  Schedule & Cancel - function ..................................................... 4,026,585.59 op/s
  Schedule & Cancel - target, function ............................................. 1,585,897.35 op/s
  Schedule & Cancel - target, string method name ................................... 1,546,175.72 op/s
  Schedule & Cancel - target, string method name, 1 argument ....................... 1,458,807.90 op/s
  Schedule & Cancel - target, string method name, 2 arguments ...................... 1,404,384.92 op/s
  Schedule & Cancel - prescheduled, same queue - target, string method name .......... 892,589.49 op/s
  Schedule & Cancel - prescheduled, separate queue - target, string method name .... 1,492,704.74 op/s
  Schedule & Flush - function ........................................................ 436,791.23 op/s
  Schedule & Flush - target, function ................................................ 426,065.59 op/s
  Schedule & Flush - target, string method name ...................................... 424,214.41 op/s
  Schedule & Flush - target, string method name, 1 argument .......................... 408,405.02 op/s
  Schedule & Flush - target, string method name, 2 arguments ......................... 402,531.98 op/s
  DEBUG - Schedule & Flush - function ................................................ 114,981.01 op/s
  DEBUG - Schedule & Flush - target, function ........................................ 114,059.11 op/s
  DEBUG - Schedule & Flush - target, string method name .............................. 113,166.18 op/s
  DEBUG - Schedule & Flush - target, string method name, 1 argument .................. 113,705.79 op/s
  DEBUG - Schedule & Flush - target, string method name, 2 arguments ................. 112,884.24 op/s
  Throttle - function .............................................................. 6,247,169.94 op/s
  Throttle & Cancel - function, target ............................................... 186,106.56 op/s
bekzod commented 6 years ago

@rwjblue ping

bekzod commented 6 years ago

ping

stefanpenner commented 6 years ago

released as v2.3.0 🎉