Improve performance of a JavaScript function (with an array lookup) -


i'm trying complete code challenge have decode keystrokes of cell phone t9 input characters create text message. main function (reverse_t9) takes string of keys such "44 444" or "999337777" , need translate them corresponding texts ("hi", or "yes" respectively).

i have logic down , can generate correct outputs, challenge telling me i'm exceeding time limit 4000ms. i've found couple spots improve performance, still can't under mark. think biggest time-waster "getletterfromdigits" function has iterate through array find corresponding mapping set of keystrokes.

am missing other obvious performance problems? please let me know if need more info.

function reverse_t9(keys) {     var retval = "";     var maxkeystrokes = 3;     var splitkeystrokes = splitkeystrokesbyspacesandkeys(keys);      (i = 0, numsplits = splitkeystrokes.length; < numsplits; i++){         //console.log("this split:");         //console.log(splitkeystrokes[i]);         //console.log("this letter:");         //console.log(getletterfromdigits(splitkeystrokes[i]));         retval = retval + getletterfromdigits(splitkeystrokes[i]);     }      return retval; }  function splitkeystrokesbyspacesandkeys(keys) {     var retval = [];     var lastkey = "";     var thiskey = "";     var lastsplit = 0;     var isspace = 0;      (i = 0, numkeys = keys.length; <= numkeys; i++) {          thiskey = keys.substring(i, + 1);          if (i == 0) {             // first time around, nothing else, assign last key             lastkey = thiskey;         } else {             if (thiskey != lastkey) {                 if (thiskey != " ") {                     if (lastkey != " ") {                         retval.push(keys.substring(lastsplit, i));                     } else {                         retval.push(keys.substring(lastsplit, - 1));                     }                      lastsplit = i;                 }                  lastkey = thiskey;              } else {                 // key did not change, assign last key , continue on                 lastkey = thiskey;             }         }     }      return retval; }  function getletterfromdigits(digits){     var retval;      var digitmapping = [     {         digit: "1",         mapping: []     },     {         digit: "2",         mapping: ["a", "b", "c"]     },     {         digit: "3",         mapping: ["d", "e", "f"]     },     {         digit: "4",         mapping: ["g", "h", "i"]     },     {         digit: "5",         mapping: ["j", "k", "l"]     },     {         digit: "6",         mapping: ["m", "n", "o"]     },     {         digit: "7",         mapping: ["p", "q", "r", "s"]     },     {         digit: "8",         mapping: ["t", "u", "v"]     },     {         digit: "9",         mapping: ["w", "x", "y", "z"]     },     {         digit: "0",         mapping: ["*"]     }      ];      var digit = digits.substring(0, 1);       (i = 0, nummappings = digitmapping.length; < nummappings; i++){         if (digitmapping[i].digit == digit){             retval = digitmapping[i].mapping[digits.length - 1];             break;         }     }      return retval; } 

first, declare digit mapping outside of function, you're reusing results of work rather performing work every time function called.

secondly, make digitmapping use key/value pairs can quick lookup based on given property name rather having loop through it.

var digitmapping =  {     "1": [],     "2": ["a", "b", "c"],     ... };  function getletterfromdigits(digits){     var digit = digits.substring(0, 1);     return digitmapping[digit][digits.length - 1]; } 

Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -