array-cursor
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -8,2 +8,4 @@ var measureSpeedSync = require('measure-speed').measureSpeedSync; | ||
console.log('Slicing an array'); | ||
console.log('================'); | ||
console.log('Array length: ', ARRAY_SIZE); | ||
@@ -40,1 +42,36 @@ console.log('Number of slices: ', SLICES); | ||
console.log('Using ArrayCursor', ms); | ||
var ARRAY_SIZE = 150000; | ||
var NITEMS = 10; | ||
console.log(''); | ||
console.log('Slicing the array and calculating the average'); | ||
console.log('============================================='); | ||
console.log('Array length: ', ARRAY_SIZE); | ||
console.log('Number of items: ', NITEMS); | ||
var array3 = getRandomArray(ARRAY_SIZE); | ||
var array4 = new ArrayCursor(getRandomArray(ARRAY_SIZE)); | ||
var ms = measureSpeedSync(function () { | ||
var result, output = []; | ||
for (var i = 0; i < ARRAY_SIZE/NITEMS; i++) { | ||
result = array3.slice(i * NITEMS, i * NITEMS + NITEMS) | ||
.reduce((acc, item) => acc + item, 0); | ||
output.push(result); | ||
} | ||
}, { samples: 10, discard: 2 }); | ||
console.log('Using real array', ms); | ||
var ms = measureSpeedSync(function () { | ||
var result, output = []; | ||
for (var i = 0; i < ARRAY_SIZE/NITEMS; i++) { | ||
result = array4.slice(i * NITEMS, i * NITEMS + NITEMS) | ||
.reduce((acc, item) => acc + item, 0); | ||
output.push(result); | ||
} | ||
}, { samples: 10, discard: 2 }); | ||
console.log('Using ArrayCursor', ms); |
45
index.js
@@ -0,1 +1,2 @@ | ||
function ArrayCursor(data, begin, end) { | ||
@@ -7,2 +8,17 @@ this.data = data; | ||
this.length = this.end > this.begin ? this.end - this.begin : 0; | ||
// if (Proxy) { | ||
// var handler = { | ||
// configurable: false, | ||
// enumerable: true, | ||
// get: function(target, name) { | ||
// var index = Number(name); | ||
// if (! isNaN(index) && index >= 0) { | ||
// return target.get(index); | ||
// } | ||
// return target[name]; | ||
// } | ||
// }; | ||
// return new Proxy(this, handler); | ||
// } | ||
}; | ||
@@ -16,3 +32,4 @@ | ||
begin = begin ? this.begin + begin : this.begin; | ||
end = end ? this.end + end : this.end; | ||
end = end || this.end; | ||
end = end > 0 ? this.begin + end : this.end + end; | ||
return new ArrayCursor(this.data, begin, end); | ||
@@ -27,2 +44,28 @@ }; | ||
ArrayCursor.prototype.map = function map(cb) { | ||
var output = []; | ||
for (var i = 0; i < this.length; i++) { | ||
output.push(cb(this.get(i), i, this)); | ||
} | ||
return output; | ||
}; | ||
ArrayCursor.prototype.filter = function map(cb) { | ||
var output = []; | ||
for (var i = 0; i < this.length; i++) { | ||
if (cb(this.get(i), i, this)) { | ||
output.push(this.get(i)); | ||
} | ||
} | ||
return output; | ||
}; | ||
ArrayCursor.prototype.reduce = function map(cb, initialValue) { | ||
var accumulator = initialValue === undefined ? this.get(0) : initialValue; | ||
for (var i = 0, len = this.length; i < len; i++) { | ||
accumulator = cb(accumulator, this.get(i), i, this); | ||
} | ||
return accumulator; | ||
}; | ||
ArrayCursor.prototype.toArray = function toArray() { | ||
@@ -29,0 +72,0 @@ return this.data.slice(this.begin, this.end); |
{ | ||
"name": "array-cursor", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "An immutable array that can sliced without actually mutating", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -46,1 +46,7 @@ Array cursor | ||
* toArray(): it returns a new array that is the slice of the array contained in this.data. | ||
* It also support various array methods: map, filter, reduce | ||
### Why not using a proxy to access data seamlessly ? | ||
As of node 8.7 proxies are so slow to instantiate that defeat completely the purpose of the library. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
52
8536
127