Skip to content

Refactor number[] to Float32Array or TypedArray for performance improvement #2

@kalwalt

Description

@kalwalt

Feature enhancement

Goal:
Replace all occurrences of JavaScript number[] arrays used as vectors in the codebase (e.g., in OneEuroFilter.ts) with Float32Array (or potentially other TypedArray types, e.g., Float64Array if higher precision is required, but Float32Array is usually sufficient for most filtering operations and is much more efficient for performance and memory).

Rationale:

  • Typed arrays like Float32Array provide more efficient memory layout and computations, especially beneficial for mathematical and signal-processing code like One Euro Filter.
  • This may improve performance, lower memory usage, and allows compatibility with libraries and APIs that expect typed arrays.
  • It also can reduce hidden bugs/inefficiencies from JavaScript's general-purpose arrays.

Proposed changes:

  • Audit and identify all uses of number[] in OneEuroFilter and related files (especially xPrev, dxPrev, vector math, filter inputs/outputs).
  • Propose and implement conversion to Float32Array where possible.
    • Ensure correct initialization and copying (e.g., new Float32Array(length) or Float32Array.from(arr))
    • Replace map and similar array methods with equivalent typed array methods as required.
    • Adjust any function signatures to accept/return the appropriate type.
    • Update tests and examples as needed.
  • Add a note in the README or JSDoc about the expected input type.

Confirmation:
Float32Array is generally preferable for numerical processing over plain number[] due to speed and efficiency, unless double precision is required (which is rare in most signal and filtering code). This should be confirmed in project context.


  • Identify all uses of number[] as vector/matrix or buffer types.
  • Propose how to convert each use to Float32Array (or another relevant TypedArray if appropriate).
  • Update the implementation accordingly and verify all tests/examples/pass.
  • Update documentation to clarify expected types.

References:


If there are any strong reasons to keep usage of number[] in some places, please document with comments. Otherwise, prefer Float32Array for optimal JavaScript performance.

Metadata

Metadata

Labels

enhancementNew feature or request
No fields configured for Feature.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions