{"ScriptPreparationCode":" var stringsearch = \u0022o\u0022, str = \u0022this is foo bar\u0022;\r\n\r\nstr2buffer = function(s){ var bu = new ArrayBuffer(s.length), aUint8 = new Uint8Array(bu );\r\n for(var i=0; i\u003Cbu.byteLength; aUint8[i]=s.charCodeAt(i),i\u002B\u002B);return aUint8;\r\n };\r\nvar bstr = str2buffer (\u0022this is foo bar\u0022)\r\n,schar = \u0027o\u0027.charCodeAt()\r\n,cnt=0;\r\n\r\nvar ubstr = \u0022this is foo bar\u0022.split(\u0027\u0027).map( function(e,i){ return e.charCodeAt();} )\r\n ,schar = \u0027o\u0027.charCodeAt()\r\n ,cnt=0;\r\nvar hist = {};\r\nvar len = str.length;\r\n\r\nfunction countChar (char, search) {\r\n \r\n let num = 0;\r\n let str = search;\r\n let pos = str.indexOf(char);\r\n \r\n while(pos \u003E -1) {\r\n str = str.slice(pos \u002B 1);\r\n pos = str.indexOf(char);\r\n num\u002B\u002B;\r\n }\r\n\r\n return num;\r\n\r\n}\r\n\r\nfunction countChar2 (char, str) {\r\n \r\n let num = 0;\r\n let pos = str.indexOf(char);\r\n \r\n while(pos \u003E -1) {\r\n pos = str.indexOf(char, pos \u002B 1);\r\n num\u002B\u002B;\r\n }\r\n\r\n return num;\r\n\r\n}","TestCases":[{"Name":"Match","Code":"(\u0022this is foo bar\u0022.match(/o/g)||[]).length;","IsDeferred":false},{"Name":"Split","Code":"\u0022this is foo bar\u0022.split(\u0022o\u0022).length-1;","IsDeferred":false},{"Name":"For loop - access with count","Code":"for(var count=-1,index=-2; index != -1; count\u002B\u002B,index=str.indexOf(stringsearch,index\u002B1));","IsDeferred":false},{"Name":"For loop - searching for a single character","Code":"for(var i=count=0; i\u003Cstr.length; count\u002B=\u002B(stringsearch===str[i\u002B\u002B]));","IsDeferred":false},{"Name":"element mapping and filtering with position ","Code":"str.split(\u0027\u0027).map( function(e,i){ if(e === \u0027o\u0027) return i;} ).filter(Boolean).length;","IsDeferred":false},{"Name":"deleting the character out of the string and measuring the distance in length","Code":"str.length - str.replace(/o/g,\u0027\u0027).length;","IsDeferred":false},{"Name":"based on typed arrays access with cnt","Code":"for(var i=0;i\u003Cbstr.byteLength;schar!==bstr[i\u002B\u002B]||cnt\u002B\u002B);","IsDeferred":false},{"Name":"based on untyped Arrays access with cnt","Code":"for(var i=0;i\u003Cubstr.length;schar!==ubstr[i\u002B\u002B]||cnt\u002B\u002B);","IsDeferred":false},{"Name":"Using reduce","Code":"str.split(\u0027\u0027).reduce(function(p,c,i,a){ if(c === schar || p === schar){return isNaN(parseInt(p))? 1:\u002Bp\u002B1;} return p;}) ","IsDeferred":false},{"Name":"Histogram dict","Code":"for (si in str) {\r\n hist[str[si]] = hist[str[si]] ? 1\u002Bhist[str[si]]:1;\r\n}","IsDeferred":false},{"Name":"split filtering with destructor","Code":"[...str].filter(l =\u003E l === \u0027o\u0027).length;","IsDeferred":false},{"Name":"Sanity check for loop","Code":"for (let charIndex = 0; charIndex \u003C len; \u002B\u002BcharIndex) {\r\n if (str[charIndex] === \u0027,\u0027) {\r\n count\u002B\u002B;\r\n }\r\n}","IsDeferred":false},{"Name":"Sanity check for loop with of","Code":"for (const char of str) {\r\n if (char === \u0027o\u0027) {\r\n count\u002B\u002B;\r\n }\r\n}","IsDeferred":false},{"Name":"For loop indexOf slicing","Code":"countChar(stringsearch, str);","IsDeferred":false},{"Name":"For loop indexOf from position","Code":"countChar2(stringsearch, str);","IsDeferred":false}]}