npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

vecmath

v0.1.4

Published

Highly optimized vector/matrix math (using objects)

Downloads

796

Readme

no longer maintained

Although classes and chainable methods can lead to cleaner code and might be easier for rapid prototyping, they ultimately are not as good for module interoperability as plain arrays. For modules I would recommend gl-vec2, gl-vec3, gl-mat4, etc. as it's more modular and generic. Examples:

https://www.npmjs.org/package/delaunay-triangulate
https://www.npmjs.org/package/simplify-path
https://www.npmjs.org/package/chaikin-smooth
https://www.npmjs.org/package/bezier
https://www.npmjs.org/package/adaptive-bezier-curve
https://www.npmjs.org/package/triangulate-contours
https://www.npmjs.org/package/bunny
https://www.npmjs.org/package/parse-svg-path

etc.

--

vecmath

This is a small vector module, built with the optimized codebase of gl-matrix but using objects and JavaScript paradigms instead of typed arrays.

install

With node:

npm install vecmath

... in code ...
var Matrix4 = require('vecmath').Matrix4;

Or you can grab the UMD build from the build folder, which will work with RequireJS or a non-module app.

performance

The reason gl-matrix is so blazingly fast is mostly because its code is highly optimized. It unrolls loops, uses inline code everywhere, and takes advantage of JS variable caching.

Thanks to optimizations on JS engines (V8, SpiderMonkey, etc), hidden classes and inline caches are really fast nowadays (and will probably only get faster). As you can see in the following benchmarks, class-based Vectors actually outperform array access:

http://jsperf.com/gl-matrix-vs-objects/4
http://jsperf.com/gl-matrix-vs-vecmath

It's important to realize that the difference between property and array accessors is negligible for the vast majority of applications, so you shouldn't be sacrificing code readability and maintainability for a change that won't make a dent in the long run.

What about WebGL?

Using a typed array (if available) for matrices was definitely the right choice of gl-matrix, and so we do the same for WebGL compatibility. Matrix objects have val, which is the backing array (Float32Array, or falls back to Array for old browsers).

var mat = new Matrix4();

gl.uniformMatrix4fv(loc, false, mat.val);

What about new objects?

Just like in gl-matrix, the API tries to encourage re-using vectors to avoid allocations. So often your code will look like this:

myVec.add( tmp.set(0, 10, 50) );

However, if you need to create new objects (out of laziness or some other reason), all functions that take a vector/quaternion type will support lightweight objects as well:

myVec.add({ x:0, y: 10, z: 50 });

These are usually a lot faster to allocate than a Vector class, or a Float32Array (in the case of gl-matrix).

If you find yourself creating a lot of objects, you should use a Pool to reduce allocations.

Extra features

The library does its best to stay consistent with the gl-matrix API, but also includes a couple extra features:

  • Vector3.project(projMatrix): this method is useful for projecting a 3D point into 2D space
  • Vector3.unproject(viewport, invProjMatrix): useful for unprojecting a 2D point into 3D space

TODO:

  • Move docs over from gl-matrix
  • Add licensing information since most of the code belongs to gl-matrix (???)
  • Add mat2 and mat2d
  • Test Quaternion + Matrix classes and compare with gl-matrix results
  • Further improvements to test suite, using mocha or something
  • Make it more modular by placing each class in its own module. Will probably be math-vector2, math-vector3, etc. since vector2 is already taken.

License

MIT