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:
Michael "Z" Goddard 2018-10-30 15:39:55 -04:00
parent 5b10d41ba3
commit ec414fffc6
No known key found for this signature in database
GPG key ID: 762CD40DD5349872

View file

@ -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;