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 Apr 21, 2023
1 parent d6e0bde commit a5ca4e4
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 71 deletions.
9 changes: 4 additions & 5 deletions packages/core/src/browser/tree/tree-iterator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// *****************************************************************************
// Copyright (C) 2017-2023 TypeFox and others.
// Copyright (C) 2017 TypeFox and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
Expand All @@ -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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// *****************************************************************************
// Copyright (C) 2018-2023 TypeFox and others.
// Copyright (C) 2018 TypeFox and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
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 a5ca4e4

Please sign in to comment.