-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace push.apply with dedicated function
- Loading branch information
Showing
16 changed files
with
125 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import defined from "./defined"; | ||
|
||
/** | ||
* Adds all elements from the given source array to the given target array. | ||
* | ||
* If the `source` is `undefined`, then nothing will be done. Otherwise, | ||
* this has the same semantics as | ||
* ``` | ||
* for (const s of source) target.push(s); | ||
* ``` | ||
* but is usually more efficient than a `for`-loop, and does not put the | ||
* elements of the source on the stack, as it would be done with the | ||
* spread operator or when using `target.push.apply(source)`. | ||
* | ||
* @function | ||
* @private | ||
* | ||
* @param {Array|undefined} source The source array | ||
* @param {Array} target The target array | ||
* | ||
* @example | ||
* const target = [ 0, 1, 2 ]; | ||
* const source = [ 3, 4, 5 ]; | ||
* Cesium.addAll(source, target); | ||
* // The target is now [ 0, 1, 2, 3, 4, 5 ] | ||
*/ | ||
function addAll(source, target) { | ||
if (!defined(source)) { | ||
return; | ||
} | ||
const s = source.length; | ||
if (s === 0) { | ||
return; | ||
} | ||
const t = target.length; | ||
target.length += s; | ||
for (let i = 0; i < s; i++) { | ||
target[t + i] = source[i]; | ||
} | ||
} | ||
export default addAll; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { addAll } from "../../index.js"; | ||
|
||
describe("Core/addAll", function () { | ||
it("works for basic arrays", function () { | ||
const source = [3, 4, 5]; | ||
const target = [0, 1, 2]; | ||
addAll(source, target); | ||
expect(target).toEqual([0, 1, 2, 3, 4, 5]); | ||
}); | ||
|
||
it("works for null source", function () { | ||
const target = [0, 1, 2]; | ||
addAll(null, target); | ||
expect(target).toEqual([0, 1, 2]); | ||
}); | ||
|
||
it("works for undefined source", function () { | ||
const target = [0, 1, 2]; | ||
addAll(undefined, target); | ||
expect(target).toEqual([0, 1, 2]); | ||
}); | ||
|
||
it("works for large arrays", function () { | ||
const source = Array(200000); | ||
const target = Array(200000); | ||
const result = Array(400000); | ||
addAll(source, target); | ||
expect(target).toEqual(result); | ||
}); | ||
}); |
Oops, something went wrong.