Changed some for loops that were using Object.keys(this.allLabels) for a for-in loop. hasOwnProperty is not checked because allLabels is an empty object so it doesn't inherit nothing via prototype
Made the following changes to the _compareLabels function:
Removed the Object.keys().map() with a simple for-in loop
Changed the order of the if conditionals to better short circuit evaluation and to just execute the _allLower function on last resort
Changed the compare function to a simple substraction, removing both ifs
Changed the setupLabels function to do the bulk insertion to the tree when possible. As seen in rbush documentation it's ~2-3 times faster than inserting items one by one
spec/labelgun.spec.js
Modified some tests to take into account the need to explicitly call the update method when the insertion is finished
Results
20 executions of the benchmark clocked at an average of 25.6 milliseconds before the optimization.
After the optimization the average is 12.3 milliseconds. Twice as fast.
Things changed
src/labelgun.js
spec/labelgun.spec.js
Results
20 executions of the benchmark clocked at an average of 25.6 milliseconds before the optimization. After the optimization the average is 12.3 milliseconds. Twice as fast.