Skip to content

Commit

Permalink
Simplify by just removing spread operator
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-fleck-at committed Feb 21, 2023
1 parent 907a614 commit 583af17
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 69 deletions.
7 changes: 3 additions & 4 deletions packages/core/src/browser/tree/tree-iterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
// *****************************************************************************

import { ArrayUtils } from '../../common';
import { CompositeTreeNode, TreeNode } from './tree';
import { TreeNode, CompositeTreeNode } from './tree';
import { ExpandableTreeNode } from './tree-expansion';

export interface TreeIterator extends Iterator<TreeNode> {
Expand Down Expand Up @@ -208,7 +207,7 @@ export namespace Iterators {
while (stack.length > 0) {
const top = stack.pop()!;
yield top;
stack = ArrayUtils.pushAll(stack, (children(top) || []).filter(include).reverse());
stack = stack.concat((children(top) || []).filter(include).reverse());
}
}

Expand All @@ -221,7 +220,7 @@ export namespace Iterators {
while (queue.length > 0) {
const head = queue.shift()!;
yield head;
queue = ArrayUtils.pushAll(queue, (children(head) || []).filter(include));
queue = queue.concat((children(head) || []).filter(include));
}
}

Expand Down
36 changes: 0 additions & 36 deletions packages/core/src/common/array-utils.spec.ts

This file was deleted.

29 changes: 0 additions & 29 deletions packages/core/src/common/array-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,33 +106,4 @@ export namespace ArrayUtils {
export function coalesce<T>(array: ReadonlyArray<T | undefined | null>): T[] {
return <T[]>array.filter(e => !!e);
}

/**
* A safe variant to push additional items to an array. By default, the
* array push operation in combination with the spread operator is used.
* However, if the callstack size is exceeded on large additions we use the
* concatenation of arrays instead as it not depend on the callstack
* size.
*
* The original array might be modified.
*
* @param array An array of elements.
* @param items Additional elements to be added to the array.
* @returns An array containing the original elements with the additional
* elements appended. This may or may not be the array that was handed in.
*/
export function pushAll<T>(array: T[], items: T[]): T[] {
try {
// typically faster but might fail depending on the number of items and the callstack size
array.push(...items);
return array;
} catch (error) {
if (error instanceof RangeError) {
// typically slower but works if we otherwise exceed the callstack size
// according to online benchmarks concat is faster than a forEach push
return array.concat(items);
}
throw error;
}
}
}

0 comments on commit 583af17

Please sign in to comment.