Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fast-push should adjust end based on start #8264

Merged
merged 1 commit into from
Oct 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});
});