JoshGlazebrook / smart-buffer

smart-buffer is a Buffer wrapper that adds automatic read & write offset tracking, string operations, data insertions, and more.
https://www.npmjs.com/package/smart-buffer
MIT License
102 stars 18 forks source link

Performance Issue #39

Open rluvaton opened 3 years ago

rluvaton commented 3 years ago

Great project man!

The only thing that concerns me is the performance issue:


  6 tests completed.

  DataView                          x 113,477 ops/sec ±5.44% (63 runs sampled)
  Buffer                            x  49,649 ops/sec ±6.65% (59 runs sampled)
  SmartBuffer.insert (fixed size)   x   2,686 ops/sec ±2.81% (78 runs sampled)
  SmartBuffer.write (fixed size)    x   7,728 ops/sec ±3.30% (72 runs sampled)
  SmartBuffer.insert (dynamic size) x   8,543 ops/sec ±1.93% (84 runs sampled)
  SmartBuffer.write (dynamic size)  x   8,178 ops/sec ±2.70% (76 runs sampled)

The benchmark code is this ReplIt

jeanbmar commented 2 years ago

The perf on the following concerns me specifically because no buffer resizing happens:

.add('SmartBuffer.write (fixed size)', function() {
    const buffer = SmartBuffer.fromBuffer(Buffer.alloc(SIZE * 2));

    for (let i = 0; i < SIZE; i++) {
      buffer.writeUInt16LE(data[i]);
    }
  })

This lib does a lot of checks on buffer operations but if the factor vs regular buffers is x7 it's definitely an issue in any context where performance matters.

JoshGlazebrook commented 2 years ago

Do you have any suggestions?

jeanbmar commented 2 years ago

I would remove all the unnecessary wrapping / checking (like stuff on offsets) to remove any overhead. Besides ensuring capacity, all the relevant checking is already performed by Node.js (e.g. https://github.com/nodejs/node/blob/2be596604ac13a4dd68eb9cc38e54f2df438dd97/lib/buffer.js#L1065). IMO, what is not checked by Node.js Buffer should be the responsibility of the user.