Browse Source

JavaScript Optimize functions (#7710)

* Optimize functions

localeCompare vs Ifelse(20% faster)

* spelling

Co-authored-by: Nate <[email protected]>
Mirodil 2 years ago
parent
commit
8def6e7a96
1 changed files with 11 additions and 1 deletions
  1. 11 1
      frameworks/JavaScript/fastify/handlers.js

+ 11 - 1
frameworks/JavaScript/fastify/handlers.js

@@ -28,7 +28,7 @@ module.exports = databaseLayer => ({
     const fortunes = await databaseLayer.allFortunes();
     const fortunes = await databaseLayer.allFortunes();
 
 
     fortunes.push(h.additionalFortune);
     fortunes.push(h.additionalFortune);
-    fortunes.sort((a, b) => a.message.localeCompare(b.message));
+    fortunes.sort(compare);
 
 
     reply.view("fortunes.hbs", { fortunes });
     reply.view("fortunes.hbs", { fortunes });
   },
   },
@@ -53,3 +53,13 @@ module.exports = databaseLayer => ({
     reply.send(worldsToUpdate);
     reply.send(worldsToUpdate);
   }
   }
 });
 });
+
+// faster than localeCompare
+function compare(a, b) {
+  if(a.message < b.message){
+    return -1;
+  } else if (a.message > b.message) {
+    return 1;
+  }
+  return 0;
+}