Coder Social home page Coder Social logo

powerarray's Introduction

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.

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).

powerarray's People

Contributors

techfort avatar homeopatchy avatar

Watchers

James Cloos avatar Inno avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.