A set of pure functions that perform common calculations on JavaScript TypedArrays

Usage no npm install needed!

<script type="module">
  import vectorTools from '';



A set of pure functions that perform common calculations on JavaScript TypedArrays.

Warning: This library is not production-ready.

This is not a particularly complete or optimised library, I just made it for my personal use as I was tired of writing the same for loops over and over. But it's super lightweight and ES6 ready so someone may enjoy it.


npm install vector-tools

Include dist/web/vector-tools.js in a <script> tag in your HTML head.


  • in node, you can import specific functions from the library:
import { vscale } from 'vector-tools';
  • or import the entire library:
import * as VectorTools from 'vector-tools';
  • in the Web, you get the VectorTools object on the window.


Functions starting with v always return a TypedArray vector. Return types of other functions and utilities are indicated below.

Number sum(a)

calculates the sum of components in vector a

Number dot(a, b)

calculates the dot product of a and b

Array<TypedArray> segment(a, seq_len)

segments the vector into ceil(a/seq_len) segments of length seq_len. The last segment may be smaller than seq_len. Returns a list of segments.

vsum(a, b)

returns component-wise sum vector of a and b.

vadd(a, x)

returns a copy of a with components increased by x.

vscale(a, x)

returns a copy of a with components scaled (multiplied) by x.

vconcat(a, b)

returns a concatenated vector of a and b.

vnormalize(a[, scale])

returns a normalized copy of a, optionally scaled by scale.


  • Array typed_to_array(a) – converts TypedArray to Array
  • TypedArray array_to_typed(a, type) – converts Array to TypedArray of type type.
  • Number randint(range) - returns a random integer between 0 and range.
  • TypedArrayConstructor get_type(a) – returns the constructor (and therefore type) of the passed TypedArray. If argument is not TypedArray, throws TypeError.


cd vector-tools
npm install

Tests are written with Jasmine, run with npm test.

To build for node, run npm run node-compile. For web (webpack), npm run web-compile. For everything, npm run build.