From 0e5473c55c1928cb10c4c137a569c699e014f19b Mon Sep 17 00:00:00 2001
From: Ivica Ico Bukvic <ico@vt.edu>
Date: Fri, 14 Aug 2020 23:27:12 -0400
Subject: [PATCH] Additional optimizations to avoid unnecessary runtime string
 comparisons for the purpose of OS detection.

---
 pd/nw/index.js |  4 ++--
 pd/nw/pdgui.js | 12 +++++++++---
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/pd/nw/index.js b/pd/nw/index.js
index cd3b94a64..2a168e27b 100644
--- a/pd/nw/index.js
+++ b/pd/nw/index.js
@@ -410,7 +410,7 @@ function nw_create_window(cid, type, width, height, xpos, ypos, attr_array) {
     } else {
         pos = null_pos;
         //pdgui.post("check_os=" + pdgui.check_os("win32"));
-        if (pdgui.check_os("linux") == 1) {
+        if (pdgui.nw_os_is_linux == 1) {
             ypos = ypos - pdgui.nw_menu_offset - 3;
         }
     }
@@ -434,7 +434,7 @@ function nw_create_window(cid, type, width, height, xpos, ypos, attr_array) {
         // altogether to simplify things. But we'd have to add some kind of
         // widget for the "Put" menu.
         // ico@vt.edu: on 0.46.2 this is now 25, go figure...
-        height: height + (pdgui.nw_menu_offset * (pdgui.check_os("darwin") == 1 ? 0 : 1)),
+        height: height + (pdgui.nw_menu_offset * !pdgui.nw_os_is_osx),
         x: xpos,
         y: ypos
     }, function (new_win) {
diff --git a/pd/nw/pdgui.js b/pd/nw/pdgui.js
index 25456f5de..4c0b269f7 100644
--- a/pd/nw/pdgui.js
+++ b/pd/nw/pdgui.js
@@ -826,10 +826,16 @@ function check_os(name) {
 
 exports.check_os = check_os;
 
-// ico@vt.edu 2020-08-14: used to speed-up window size consistency operations
-// in index.js' nw_create_window and pdgui.js' canvas_check_geometry
+// ico@vt.edu 2020-08-14: used to speed-up window size consistency and other
+// OS-centric operations by avoiding constant calls to string comparisons.
+// Most pertinent calls can be found in index.js' nw_create_window and pdgui.js'
+// canvas_check_geometry
+var nw_os_is_linux = check_os("linux");
+var nw_os_is_osx = check_os("darwin");
 var nw_os_is_windows = check_os("win32");
 
+exports.nw_os_is_linux = nw_os_is_linux;
+exports.nw_os_is_osx = nw_os_is_osx;
 exports.nw_os_is_windows = nw_os_is_windows;
 
 // ico@vt.edu 2020-08-11: this appears to have to be 25 at all times
@@ -864,7 +870,7 @@ function canvas_check_geometry(cid) {
         // ico@vt.edu in 0.46.2 this is now 25 pixels, so I guess
         // it is now officially kludge^2
         win_h = patchwin[cid].height - 
-            (nw_menu_offset * (check_os("darwin") == 1 ? 0 : 1)),
+            (nw_menu_offset * !nw_os_is_osx),
         win_x = patchwin[cid].x,
         win_y = patchwin[cid].y,
         cnv_width = patchwin[cid].window.innerWidth,
-- 
GitLab