invertase / denque

The fastest javascript implementation of a double-ended queue. Used by the official Redis, MongoDB, MariaDB & MySQL libraries for Node.js and many other libraries.
https://docs.page/invertase/denque
Apache License 2.0
354 stars 33 forks source link

Optimizations for toArray and fromArray #46

Closed dnlmlr closed 2 years ago

dnlmlr commented 2 years ago

I made some further optimizations to the _copyArray function that I missed last time (#43) and also optimized the _fromArray function. This in turn provides a large improvement when initializing the Denque from an array and also in some situations when exporting to an array.

Creating the queue from an array with the new preallocating code avoids growth and is a significant improvement on large arrays:

Using Array.slice in _copyArray to create copies with the same size on contiguous buffer data is also a pretty good performance boost in situations where it applies. This applies in every situation where head < tail. Not sure why but before when head < tail and head != 0, is was noticeably slow. This is now fixed

The full benchmark results and scripts for validation is again found in the benchmark repository

PS: The _fromArray optimization adds a _nextPowerOf2 function that calculates the next power of 2 to directly allocate the buffer with the correct size. This could be used to implement #42.

codecov[bot] commented 2 years ago

Codecov Report

Merging #46 (a7fce30) into master (f400043) will not change coverage. The diff coverage is 100.00%.

:exclamation: Current head a7fce30 differs from pull request most recent head 61812bf. Consider uploading reports for the commit 61812bf to get more accurate results

@@            Coverage Diff            @@
##            master       #46   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines          224       237   +13     
  Branches        48        49    +1     
=========================================
+ Hits           224       237   +13     
Impacted Files Coverage Δ
index.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update f400043...61812bf. Read the comment docs.