From 5f7fe9096b5d3feee76cde634873e56b15003464 Mon Sep 17 00:00:00 2001 From: Neil Fraser Date: Fri, 4 Jun 2021 06:45:27 -0700 Subject: [PATCH] Use String() over toLocaleString when possible toLocaleString results in a 4.6% performance penalty when loading the Spaghetti test in Playground. --- core/field_number.js | 8 +++++++- demos/code/msg/en.js | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/core/field_number.js b/core/field_number.js index 3ea3ef22a..2cfa2ca23 100644 --- a/core/field_number.js +++ b/core/field_number.js @@ -223,7 +223,13 @@ Blockly.FieldNumber.prototype.setPrecision = function(precision) { */ Blockly.FieldNumber.prototype.setPrecisionInternal_ = function(precision) { this.precision_ = Number(precision) || 0; - var precisionString = this.precision_.toLocaleString('en-US', {maximumFractionDigits: 20}); + var precisionString = String(this.precision_); + if (precisionString.indexOf('e') != -1) { + // String() is fast. But it turns .0000001 into '1e-7'. + // Use the much slower toLocaleString to access all the digits. + precisionString = + this.precision_.toLocaleString('en-US', {maximumFractionDigits: 20}); + } var decimalIndex = precisionString.indexOf('.'); if (decimalIndex == -1) { // If the precision is 0 (float) allow any number of decimals, diff --git a/demos/code/msg/en.js b/demos/code/msg/en.js index d853826f0..113b84639 100644 --- a/demos/code/msg/en.js +++ b/demos/code/msg/en.js @@ -17,7 +17,7 @@ var MSG = { listVariable: "list", textVariable: "text", httpRequestError: "There was a problem with the request.", - linkAlert: "Share your blocks with this public link. We\'ll delete them if not used for a year. They are not associated with your account and handled as per Google\'s Privacy Policy. Please be sure not to include any private information.:\n\n%1", + linkAlert: "Share your blocks with this public link. We\'ll delete them if not used for a year. They are not associated with your account and handled as per Google\'s Privacy Policy. Please be sure not to include any private information.\n\n%1", hashError: "Sorry, '%1' doesn't correspond with any saved program.", xmlError: "Could not load your saved file. Perhaps it was created with a different version of Blockly?", badXml: "Error parsing XML:\n%1\n\nSelect 'OK' to abandon your changes or 'Cancel' to further edit the XML."