Skip to content

Commit

Permalink
fix: fast-push should adjust end based on start
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired committed Oct 28, 2022
1 parent fd3ccb3 commit 809deb5
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function fastPush<T>(target: T[], source: T[]) {
let newLength = source.length;
while (newLength - startLength > SLICE_BATCH_SIZE) {
// eslint-disable-next-line prefer-spread
target.push.apply(target, source.slice(startLength, SLICE_BATCH_SIZE));
target.push.apply(target, source.slice(startLength, startLength + SLICE_BATCH_SIZE));
startLength += SLICE_BATCH_SIZE;
}
// eslint-disable-next-line prefer-spread
Expand Down
70 changes: 70 additions & 0 deletions tests/main/tests/unit/fast-push-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { module, test } from 'qunit';

import { fastPush } from '@ember-data/store/-private';

const SLICE_BATCH_SIZE = 1200;

module('unit | fast-push', function () {
test('works as expected with 0 elements', function (assert) {
const target = [];
const source = Object.freeze([]);

fastPush(target, source);

assert.strictEqual(target.length, 0, 'no elements are in the array');
});

test('works as expected with < SLICE_BATCH_SIZE elements', function (assert) {
const target = [];
const source = Object.freeze([1, 2, 3, 4]);

fastPush(target, source);

assert.strictEqual(target.length, 4, 'four elements are in the array');
assert.deepEqual(target, source, 'the arrays are copies');
});

test('works as expected with === SLICE_BATCH_SIZE elements', function (assert) {
const target = [];
const source = Object.freeze(new Array(SLICE_BATCH_SIZE).fill(0).map((v, i) => i));

fastPush(target, source);

assert.strictEqual(target.length, SLICE_BATCH_SIZE, `${SLICE_BATCH_SIZE} elements are in the array`);
assert.deepEqual(target, source, 'the arrays are copies');
});

test('works as expected with > SLICE_BATCH_SIZE elements', function (assert) {
const target = [];
const source = Object.freeze(new Array(SLICE_BATCH_SIZE + 1).fill(0).map((v, i) => i));

fastPush(target, source);

assert.strictEqual(target.length, SLICE_BATCH_SIZE + 1, `${SLICE_BATCH_SIZE + 1} elements are in the array`);
assert.deepEqual(target, source, 'the arrays are copies');
});

test('works as expected with 2*SLICE_BATCH_SIZE elements', function (assert) {
const target = [];
const source = Object.freeze(new Array(SLICE_BATCH_SIZE * 2).fill(0).map((v, i) => i));

fastPush(target, source);

assert.strictEqual(target.length, SLICE_BATCH_SIZE * 2, `${SLICE_BATCH_SIZE * 2} elements are in the array`);
assert.deepEqual(target, source, 'the arrays are copies');
});

test('works as expected with > 2*SLICE_BATCH_SIZE elements', function (assert) {
const target = [];
const source = Object.freeze(new Array(SLICE_BATCH_SIZE * 2 + 1).fill(0).map((v, i) => i));

fastPush(target, source);

assert.strictEqual(
target.length,
SLICE_BATCH_SIZE * 2 + 1,
`${SLICE_BATCH_SIZE * 2 + 1} elements are in the array`
);
assert.deepEqual(target, source, 'the arrays are copies');
});
});

0 comments on commit 809deb5

Please sign in to comment.