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

powerarray

v0.1.2

Published

improved native array

Downloads

2

Readme

PowerArray

Turns out that you can re-write some of the methods of Array to obtain a much better performance than the native methods. In particular, Array.forEach seems to perform pretty badly.

Note: the overridden methods of PowerArray break compliance, the focus is on performance so take a look at the caveat section below.

It looks as if a for loop with cached length is the fastest way of iterating.

var i, len = array.length;
for (i = 0; i < len; i += 1) {
  someFun(array[i]);
}

So I rewrote the Array class as PowerArray and implemented the above mechanism in PowerArray.forEach with surprising results.

The results are as follows: PowerArray.forEach is averagely 5 times faster than native Array.

This is only a proof of concept.

Install with npm install powerarray

Proposed Usage

Particularly useful for arrays that need processing on all elements often, or for numeric arrays utilized as indexes for Collections of data.

Methods

All Array native methods are available through PowerArray. The following methods are either extending or overriding the native Array class.

PowerArray.forEach: utilizes a for loop for iteration, takes a callback which receives an element and the index of that element.

PowerArray.map: utilizes a for loop to return a PowerArray of mapped values, takes a callback processing function argument.

PowerArray.binarySearch: performs a binary search on the elements of the array, only relevant if the array only consists of numbers. Thanks to Oliver Caldwell's post for a quick version of the algorithm. Also note the contribution of Yehonatan and other authors of comments to the post which helped to optimise the implementation of binary search further.

PowerArray.numericSort: sorts array (if array only contains integers), useful for utilizing binarySearch. Optional sorting function argument.

PowerArray.addhAndSort: adds a new value and sorts the array automatically

Contribution

Pull requests are more than welcome, just make sure to add a test in tests/test.js (and that it passes it obviously).

Caveats

Thanks to David Souther for documenting these:

  1. No this context in fn calls, handle your own binding.
  2. No determination if i is a member of PowerArray (eg for sparse arrays, [2, 4, , 6])
  3. No exception is thrown when the callback isn't callable.

There may be more, please feel free to flag those or include them yourself through a pull request.