Skip to content

Commit 7255134

Browse files
committed
push is slow so use indices, and use native methods first. make map more generic.
1 parent c3ef7ab commit 7255134

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

src/core.js

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var slice = Array.prototype.slice,
1414
descNum = function( a, b ) { return b - a; },
1515

1616
// test if array
17-
isArray = function( arg ) {
17+
isArray = Array.isArray || function( arg ) {
1818
return toString.call( arg ) === "[object Array]";
1919
},
2020

@@ -42,15 +42,15 @@ jStat.fn = jStat.prototype = {
4242
if ( isArray( args[0] )) {
4343
if ( isArray( args[0][0] )) {
4444
for ( var i = 0; i < args[0].length; i++ ) {
45-
this.push( args[0][i] );
45+
this[i] = args[0][i];
4646
}
4747
} else {
48-
this.push( args[0] );
48+
this[0] = args[0];
4949
}
5050

5151
// if first argument is number, assume creation of sequence
5252
} else if ( !isNaN( args[0] )) {
53-
this.push( jStat.seq.apply( null, args ));
53+
this[0] = jStat.seq.apply( null, args );
5454
}
5555
return this;
5656
},
@@ -223,18 +223,12 @@ jStat.extend({
223223

224224
// map a function to a matrix or vector
225225
map : function( arr, func, toAlter ) {
226-
arr = isArray( arr[0] ) ? arr : [ arr ];
227-
var row = 0,
228-
nrow = arr.length,
229-
ncol = arr[0].length,
226+
var len = arr.length,
230227
res = toAlter ? arr : [],
231-
col;
232-
for ( ; row < nrow; row++ ) {
233-
res[row] = res[row] || [];
234-
for ( col = 0; col < ncol; col++ ) {
235-
res[row][col] = func( arr[row][col], row, col );
236-
}
237-
}
228+
i = 0;
229+
for ( ; i < len; i++ )
230+
if ( isArray( arr[i] )) res[i] = jStat.map( arr[i], func, toAlter );
231+
else res[i] = func( arr[i], i, arr );
238232
return res;
239233
},
240234

0 commit comments

Comments
 (0)