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 🙏

© 2024 – Pkg Stats / Ryan Hefner

boostarray

v0.0.3

Published

Booster shot for Javascript Arrays

Downloads

5

Readme

BoostArray

Booster shot for JavaScript Arrays

Adds "boosted" iterator methods to ordinary JavaScript arrays.

Note: BoostArray is experimental. Feedback is appreciated. Please see techfort/PowerArray for an alternative approach.

Introduction

The fastest way to iterate over an array in JavaScript is always going to be writing out the explicit for or while loop like this[citation needed]:

for (var i = 0, len = myArray.length; i < len; i++) {
	myFunc(myArray[i]);
}

However, nothing beats the convenience of JavaScript's forEach and sibling functions (map, reduce, filter):

myArray.forEach(myFunc);

However convenience comes at a cost. forEach, and siblings, do various type checking and have various extra features not needed 93%[based on very unscientific analysis of my own code] of the time. These features, desired in many cases, slow the looping process down significantly (see benchmarks below).

BoostArray is like a Booster shot for JavaScript Arrays in that it adds additional fast versions of forEach and siblings to a ordinary JavaScript arrays or, optionally, all native arrays (see usage below). Switching between the standard method and the similar boosted method can be as simple as adding a single character:

myArray.$forEach(myFunc);

Usage

There are three ways to use BoostArray:

As a boost for ordinary JavaScript arrays (recommended method)

var boostedArray = BoostArray();

/* ...add some elements using push(), etc */

boostedArray.$forEach(myFunc);

boostedArray is initially an empty array that has the boosted methods attached (see methods below). Note that while BoostArray looks (and sort of acts) like a constructor it... it really isn't (see here). BoostArray adds methods to existing arrays, but in the example above, since no array is passed to the "constructor" so BoostArray assumes you want a new array. The following method for boosting an existing array is recommended:

BoostArray(myArray);
myArray.$forEach(myFunc);

Note: using this method, any array returned from Array.prototype or BoostArray.prototype methods will be an ordinary JavaScript array. You will need subsequently to boost any array's returned from these methods (if you want to later use the boosted methods).

BoostArray(myArray);
var resultArray = BoostArray(myArray.$map(myFunc));

As a boost for all arrays (use with caution)

BoostArray(Array.prototype);

/* ... */

myArray.$forEach(myFunc);

This will boost all JavaScript arrays. All JavaScript arrays will now have the fast "boosted" methods.

Note: In general extending JavaScript natives is not recommended (see Extending JavaScript Natives) and may break other libraries. Use with caution.

As a boosting toolset

BoostArray.prototype.$forEach.call(myArray, myFunc);

Using JavaScript's Function.prototype.call() method the BoostArray methods can be applied to any array or array like object.

Methods

A boosted array (by any of first two methods above) is still an ordinary JavaScript Array. Therefore it contains all the standard Array.prototype methods.

BoostArray.isBoostedArray(element)

Returns true if an element is a boosted array, if not false.

Accessor methods

In addition to Array.prototype accessor methods a boosted array contains the following additional fast methods.

Notice the dollar sign for boosted methods.

BoostArray.prototype.$indexOf(searchElement)

Returns the first index of an element within the array that is strictly equal (the same method used by the === operator) to the searchElement, or -1 if none is found.

Note: Unlike Array.prototype.indexOf there is no fromIndex used start the search at, there is no handling of sparse arrays.

Iteration methods

In addition to Array.prototype iteration methods a boosted array contains the following additional fast methods.

Notice the dollar sign for boosted methods.

BoostArray.prototype.$forEach(callback)

Calls the callback function for each element in the array in ascending order.

Note: Unlike Array.prototype.forEach there is no thisArg used for callback binding, there is no handling of sparse arrays, and the callback function is invoked with only the element value (i.e. no element index or reference to the original array).

BoostArray.prototype.$filter(callback)

Creates a new ordinary array with all of the elements of this array for which the provided filtering function returns true.

Note: Unlike Array.prototype.filter there is no thisArg used for callback binding, there is no handling of sparse arrays, and the callback function is invoked with only the element value (i.e. no element index or reference to the original array). Also note that unless you have boosted the Array.prototype as discussed above the returned value will not have the boosted methods attached.

BoostArray.prototype.$map(callback)

Creates a new ordinary array with the results of calling a provided function on every element in this array.

Note: Unlike Array.prototype.map there is no thisArg used for callback binding, there is no handling of sparse arrays, and the callback function is invoked with only the element value (i.e. no element index or reference to the original array). Also note that unless you have boosted the Array.prototype as discussed above the returned value will not have the boosted methods attached.

BoostArray.prototype.$reduce(callback, initialValue)

Apply a the callback function against each value of the array (in ascending order) reducing it to a single value.

Note: Unlike Array.prototype.reduce the initialValue is not optional, there is no handling of sparse arrays, and the callback function is invoked with only the element value (i.e. no element index or reference to the original array).

Benchmarks

For benchmarks see Hypercubed/ArraySpeedTests

Contributions

Pull requests are appreciated. However, remember keep it fast by:

  1. no callback binding,
  2. no type checking,
  3. no element index or reference to the original array.

Acknowledgements

BoostArray was inspired by PowerArray by Joe Minichino, fast.js, ramda, and lodash. Much of the Array sub-classing code based on Extending JavaScript Arrays While Keeping Native Bracket-Notation Functionality by Ben Nadel.

License

2014 Jayson Harshbarger

MIT License