nodejs / nodejs/performance

Buffer.(read|write)(U)Int(LE|BE)(8|16|32)

Open
#2 8 comments 0 reactions 1 assignee View on GitHub

@ronag is already working on this.

Since Jan 9, 2023.

Dominant language
Shell
Stars
399
Forks
11
Avg merge
29m
Merged PRs (30d)
1

Description

Just adding something that I've been looking at which could maybe be improved.

Using Node Buffer vs Int32Array to read/write 32 bit values to buffers.

NAME                           REPS
-----------------------------------
Buffer                   64,553,382
Int32Array              174,148,855
const arrayBuffer = new ArrayBuffer(NUM_CASES * 4)
const buffer = Buffer.from(arrayBuffer)
const buffer32 = new Int32Array(arrayBuffer)

{
  Buffer: (index, value) => {
    buffer.writeInt32LE(value, index * 4)
    assert(buffer.readInt32LE(index * 4) === value)
  },
  Int32Array: (index, value) => {
    buffer32[index] = value
    assert(buffer32[index] === value)
  },
  DataView: () => {
    const c = cases[caseIndex]
    view.setInt32(caseIndex * 4, c, true)
    assert(view.getInt32(caseIndex * 4, true) === c)
    if (++caseIndex === NUM_CASES) {
      caseIndex = 0
    }
  },
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.