mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-12 06:21:29 +00:00
Optimize array functional loops (#359)
This commit is contained in:
@ -74,7 +74,7 @@ export class Array<T> {
|
||||
|
||||
every(callbackfn: (element: T, index: i32, array: Array<T>) => bool): bool {
|
||||
var buffer = this.buffer_;
|
||||
for (let index = 0, toIndex = this.length_; index < toIndex && index < this.length_; ++index) {
|
||||
for (let index = 0, toIndex = this.length_; index < min(toIndex, this.length_); ++index) {
|
||||
if (!callbackfn(LOAD<T>(buffer, index), index, this)) return false;
|
||||
}
|
||||
return true;
|
||||
@ -82,7 +82,7 @@ export class Array<T> {
|
||||
|
||||
findIndex(predicate: (element: T, index: i32, array: Array<T>) => bool): i32 {
|
||||
var buffer = this.buffer_;
|
||||
for (let index = 0, toIndex = this.length_; index < toIndex && index < this.length_; ++index) {
|
||||
for (let index = 0, toIndex = this.length_; index < min(toIndex, this.length_); ++index) {
|
||||
if (predicate(LOAD<T>(buffer, index), index, this)) return index;
|
||||
}
|
||||
return -1;
|
||||
@ -262,7 +262,7 @@ export class Array<T> {
|
||||
var length = this.length_;
|
||||
var result = new Array<U>(length);
|
||||
var resultBuffer = result.buffer_;
|
||||
for (let index = 0; index < length && index < this.length_; ++index) {
|
||||
for (let index = 0; index < min(length, this.length_); ++index) {
|
||||
STORE<U>(resultBuffer, index, callbackfn(LOAD<T>(buffer, index), index, this));
|
||||
}
|
||||
return result;
|
||||
@ -270,9 +270,8 @@ export class Array<T> {
|
||||
|
||||
filter(callbackfn: (value: T, index: i32, array: Array<T>) => bool): Array<T> {
|
||||
var buffer = this.buffer_;
|
||||
var length = this.length_;
|
||||
var result = new Array<T>();
|
||||
for (let index = 0; index < length && index < this.length_; ++index) {
|
||||
for (let index = 0, toIndex = this.length_; index < min(toIndex, this.length_); ++index) {
|
||||
let value = LOAD<T>(buffer, index);
|
||||
if (callbackfn(value, index, this)) result.push(value);
|
||||
}
|
||||
@ -285,7 +284,7 @@ export class Array<T> {
|
||||
): U {
|
||||
var accum = initialValue;
|
||||
var buffer = this.buffer_;
|
||||
for (let index = 0, toIndex = this.length_; index < toIndex && index < this.length_; ++index) {
|
||||
for (let index = 0, toIndex = this.length_; index < min(toIndex, this.length_); ++index) {
|
||||
accum = callbackfn(accum, LOAD<T>(buffer, index), index, this);
|
||||
}
|
||||
return accum;
|
||||
@ -321,7 +320,7 @@ export class Array<T> {
|
||||
|
||||
some(callbackfn: (element: T, index: i32, array: Array<T>) => bool): bool {
|
||||
var buffer = this.buffer_;
|
||||
for (let index = 0, toIndex = this.length_; index < toIndex && index < this.length_; ++index) {
|
||||
for (let index = 0, toIndex = this.length_; index < min(toIndex, this.length_); ++index) {
|
||||
if (callbackfn(LOAD<T>(buffer, index), index, this)) return true;
|
||||
}
|
||||
return false;
|
||||
|
Reference in New Issue
Block a user