From 5b10d41ba32f321a94aac4a6fb39656cfca4a2f0 Mon Sep 17 00:00:00 2001
From: "Michael \"Z\" Goddard" <mzgoddard@gmail.com>
Date: Tue, 30 Oct 2018 11:26:35 -0400
Subject: [PATCH] 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.
---
 src/util/cast.js | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/src/util/cast.js b/src/util/cast.js
index 250132905..8102f04af 100644
--- a/src/util/cast.js
+++ b/src/util/cast.js
@@ -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.