[mixins/pushAll] efficiently push an entire array into another

This commit is contained in:
Spencer Alger 2015-05-06 11:03:34 -07:00
parent e4d570d2f5
commit abda2d08e5
3 changed files with 31 additions and 1 deletions

View file

@ -295,5 +295,19 @@ define(function (require) {
return list;
},
pushAll: function (source, dest) {
var start = dest.length;
var adding = source.length;
// allocate - http://goo.gl/e2i0S0
dest.length = start + adding;
// fill sparse positions
var i = -1;
while (++i < adding) dest[start + i] = source[i];
return dest;
}
};
});

View file

@ -0,0 +1,15 @@
define(function (require) {
return ['_.pushAll', function () {
var _ = require('lodash');
it('pushes an entire array into another', function () {
var a = [1, 2, 3, 4];
var b = [5, 6, 7, 8];
var output = _.pushAll(b, a);
expect(output).to.be(a);
expect(a).to.eql([1, 2, 3, 4, 5, 6, 7, 8]);
expect(b).to.eql([5, 6, 7, 8]);
});
}];
});

View file

@ -2,6 +2,7 @@ define(function (require) {
describe('lodash mixins', function () {
run(require('specs/utils/mixins/_move'));
run(require('specs/utils/mixins/_organize_by'));
run(require('specs/utils/mixins/_push_all'));
function run(m) { describe(m[0], m[1]); }
});
});
});