lemire / FastBitSet.js

Speed-optimized BitSet implementation for modern browsers and JavaScript engines
Apache License 2.0
158 stars 19 forks source link

Using the DataView for possible performance gain #6

Closed oiime closed 5 years ago

oiime commented 5 years ago

Hey,

Have you considered using the DataView type to store the bitset when available?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView

lemire commented 5 years ago

It is an interesting approach which I did not know about... But...

Why would that be useful here?

oiime commented 5 years ago

Using values in a TypedArray is faster for allocation and setting and has a minimal memory footprint, it can also be initialized with 0 with no additional cost and has operations such as set() which can help manipulate arrays

for (let x = 1; x <= 100000; x++) {
  const arr = new Array(10000)
  // arr.fill(0)
  for (let i = 0; i <= 10000; i++) {
    arr[i] = 128
  }
}

takes ~4s

for (let x = 1; x <= 100000; x++) {
  const arr = new Uint8Array(10000)
  arr.fill(0)
  for (let i = 0; i <= 10000; i++) {
    arr[i] = 128
  }
}

takes ~0.8s

lemire commented 5 years ago

Please see... https://github.com/lemire/TypedFastBitSet.js

TypeFastBitSet uses a Uint32Array.

oiime commented 5 years ago

cool, thanks. would you mind if i take your code and do some major refactoring? i want to add some functionality and change the API

lemire commented 5 years ago

Sure you can. It is open source.