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

View file

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