mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-24 06:52:40 -05:00
check if toNumber is given a number and shortcut if so
If toNumber is called on a number avoiding passing the number to Number can provide a small performance improvement.
This commit is contained in:
parent
de86eb3f19
commit
5b10d41ba3
1 changed files with 11 additions and 0 deletions
|
@ -20,6 +20,17 @@ class Cast {
|
|||
* @return {number} The Scratch-casted number value.
|
||||
*/
|
||||
static toNumber (value) {
|
||||
// If value is already a number we don't need to coerce it with
|
||||
// Number().
|
||||
if (typeof value === 'number') {
|
||||
// Scratch treats NaN as 0, when needed as a number.
|
||||
// E.g., 0 + NaN -> 0.
|
||||
if (isNaN(value)) {
|
||||
return 0;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
const n = Number(value);
|
||||
if (isNaN(n)) {
|
||||
// Scratch treats NaN as 0, when needed as a number.
|
||||
|
|
Loading…
Reference in a new issue