Skip to content

PowerArray constructor is too slow. #22

@Hypercubed

Description

@Hypercubed

Intro

I've put together some Array speed tests for testing the speed of various "fast" array methodologies. I've been trying to implement fast version of concat in PowerArray. However, the PowerArray constructor is too slow. This in turn makes methods that return arrays (map, filter, concat) slow. The constructor is slow, in part, because of the cost of creating a new object (in other words new PowerArray() is slower new Array() which is slower than []) and also because recasting an existing array as a PowerArray requires copying each element to the new object. My approach in Hypercubed/BoostArray is to attach BoostArray prototype methods to normal arrays (or to the Array prototype), therefore never/rarely creating new objects. This method will not work in PowerArray which must create a new instance (different philosophies between the two projects).

Proposal

Therefore, I propose a new constructor in PowerArray based on the very detailed article How ECMAScript 5 still does not allow to subclass array by kangax.

function PowerArray() {
    var arr = [];
    arr.push.apply(arr, arguments);
    arr.__proto__ = PowerArray.prototype;
    return arr;
}

or the modified version consistent with current implementation.

function PowerArray(array) {
    var load = (arguments.length > 0) ? array.slice(0) : [];
    load.__proto__ = PowerArray.prototype;
    return load;
}

Proposed PowerArray constructor vs. existing PowerArray constructor

Pros:

  • Fast version of concat, slice.
  • Fast conversion of Array to PowerArray.

Cons:

  • non-standard proto property

PowerArray vs. BoostArray:

Pros:

  • Unlike BoostArray(), the PowerArray() constructor does not alter the original array, returning a new object.
  • Uses standard method names (.forEach, .map, .filter).

Cons:

  • "Constructor" is slower.
  • Requires slower PowerArray constructor in every prototype method.
  • No option to add fast methods to Array.prototype.
  • No standard compliant Array.prototype methods. (BoostArray has both compliant .forEach and faster .$forEach)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions