mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-24 15:02:52 -05:00
use Number.isNaN in Cast.toNumber
Number.isNaN does not coerce and may help performance since we either do not need to coerce the value or already have with Number.
This commit is contained in:
parent
5b10d41ba3
commit
ec414fffc6
1 changed files with 10 additions and 2 deletions
|
@ -1,5 +1,13 @@
|
||||||
const Color = require('../util/color');
|
const Color = require('../util/color');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Store and possibly polyfill Number.isNaN. Number.isNaN can save time over
|
||||||
|
* self.isNaN by not coercing its input. We need to polyfill it to support
|
||||||
|
* Internet Explorer.
|
||||||
|
* @const
|
||||||
|
*/
|
||||||
|
const _NumberIsNaN = Number.isNaN || isNaN;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @fileoverview
|
* @fileoverview
|
||||||
* Utilities for casting and comparing Scratch data-types.
|
* Utilities for casting and comparing Scratch data-types.
|
||||||
|
@ -25,14 +33,14 @@ class Cast {
|
||||||
if (typeof value === 'number') {
|
if (typeof value === 'number') {
|
||||||
// Scratch treats NaN as 0, when needed as a number.
|
// Scratch treats NaN as 0, when needed as a number.
|
||||||
// E.g., 0 + NaN -> 0.
|
// E.g., 0 + NaN -> 0.
|
||||||
if (isNaN(value)) {
|
if (_NumberIsNaN(value)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
const n = Number(value);
|
const n = Number(value);
|
||||||
if (isNaN(n)) {
|
if (_NumberIsNaN(n)) {
|
||||||
// Scratch treats NaN as 0, when needed as a number.
|
// Scratch treats NaN as 0, when needed as a number.
|
||||||
// E.g., 0 + NaN -> 0.
|
// E.g., 0 + NaN -> 0.
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Reference in a new issue