Optimization of flatten 0 case (#1621)

* Optimizationf or flatten 0 case

* Remove wayward .123deno
This commit is contained in:
Jaco
2022-08-03 10:57:24 +03:00
committed by GitHub
parent 515a77781f
commit c6e18b1a9e
4 changed files with 31 additions and 2 deletions
+8
View File
@@ -1,5 +1,13 @@
# CHANGELOG
## master
Changes:
- Optimization for empty `arrayFlatten` case
- Coding cleanups for logger (no `as` casting)
## 10.1.2 Jul 29, 2022
Changes:
View File
+19
View File
@@ -1,12 +1,31 @@
// Copyright 2017-2022 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { perf } from '../test/performance';
import { arrayFlatten } from '.';
const PERF_ONE = [[1, 2, 3, 4, 5]];
const PERF_MUL = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
describe('arrayFlatten', (): void => {
it('flattens arrays', (): void => {
expect(
arrayFlatten([[0], [1, 2, 3], [4, 5], [6], [], [7, 8]])
).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8]);
});
it('flattens a single entry', (): void => {
expect(
arrayFlatten([[1, 2, 3, 4, 5]])
).toEqual([1, 2, 3, 4, 5]);
});
it('flattens an empty', (): void => {
expect(
arrayFlatten([])
).toEqual([]);
});
perf('arrayFlatten (single)', 10_000_000, [[PERF_ONE]], arrayFlatten);
perf('arrayFlatten (multi)', 700_000, [[PERF_MUL]], arrayFlatten);
});
+4 -2
View File
@@ -19,8 +19,10 @@
* ```
*/
export function arrayFlatten <T> (arrays: readonly T[][]): T[] {
// noop for the single-entry case
if (arrays.length === 1) {
// noop for the empty & single-entry case
if (arrays.length === 0) {
return [];
} else if (arrays.length === 1) {
return arrays[0];
}