Continue to push the boundaries of performance that javascript solution to problems can deliver in a high-level language, that only be achieved in more tedious assemble, low level c/C++ will get you.
The proposal is to improve the flow control statements in Loops e.g for, while, do, which allows for better-optermized solutions to problems, avoiding some tricks, to do the same, which slight would defeat the purpose, as tricks come with hidden cpu cycle overhears.
I will provide examples of solution that should generate more optimize assembly code and have faster run times for early kick out solution, where there can be less boiler plat to handle early kickout and positive cases.
Propose that with in a since function block scope, to keep it simple at first, later it could possible be expanded to indefinite level, however, that would result in language become too complex.
Proposal 1:
Allow changing of break, continue, skip in sequence, to control multiple loops and not just the most inner loop, from with in the most inner loop.
e.g
for(let i = 0; i < 10; i++) {
for(let j =0;j<10;j++) {
console.log(i + ":" + j+",");
if (j ==5) {
break;
continue;
}
}
console.log("Tail assume correct count increments or similar code");
}
output: 1:1,1:2,1:3,1:4,1:5,2:1,2:2,2:3,2:4,2:5,....
Proposal 2:
Block skip operator, which allows for a section of code to totally be skip, such that a solution can have multiple manual typed sections, where not all may apply to the current iteration evaluation, so want them to be skipped. This could be achieved with map of functions to be call based on a condition, but this adds additional complexity and low-level code generation. This is my mind would allow process to better skip instructions and branch predict, than over head of function calling may have, but probably with compiler optermizations results in the same optermization of code block of all if statements for skips instruction being hoisted, and then it running the blocks.
for (let i = 0; i < 10; i++)
{
{
skip;
console.log("First Block,");
}
{
console.log("Second Block,");
}
}
output: Second Block,Second Block,Second Block,Second Block,Second Block,Second Block,...
Proposal 3:
Allow parametrized version of a break, continue, skip that take either an integer or label to control the exit of multiple neasted loops. The integer version say how many loops to skip, not present current loop, if larger than 0, it will skip, break or continue until that many times until the count has decrease to 0.
e.g number
for (let i = 0; i < 5; i++) {
for (let j = 0; j < 5; i++) {
for (let j = 0; j < 5; i++) {
console.log("K");
break(3);
}
}
}
output: K
e.g label
:label-first-loop:
for (let i = 0; i < 5; i++) {
:label-second-loop
for (let j = 0; j < 5; i++) {
:label-third-loop
for (let j = 0; j < 5; i++) {
console.log("K");
break(label-first-loop);
label-first-block: {...}
label-second-block: {....}
label-third-block: {....}
}
}
}
output: K
By doing this one can write much better solutions, like the following below:
function johnMary(str) {
const strJohn = "John"; // may not have null terminating as also check as literal charater.
const strMary = "Mary";
const strWasley = "Wasley";
//str += '\u0000';
let countJohn = 0;
let countMary = str.length;
let countWasley = 0;
for(let i = 0; i < str.length; i++) {
(function() { // or lamda.
if (str[i] !== strJohn[0]) { // asumption of no empty strings, which incertain langauges are represented by \0\0 incase of other end condition optermization.
return; //skip, basically goto end of parent block of brace code, this would allow for code after this doing a second string comparision to execute.
// skip, break, continue should be parametised, which take in a number e.g skip(2), will skip to blocks.
//*** alternative to return would be skip, which allows us not to use a anonmas function wrapper and overhead.***
}
let end = i + strJohn.length;
// In javascript it looks like one is quite luck, as the comparison of arr[-1] == arr[-1] => undefined === is false, so there is no additioanl evaluation.
// So I guess on needs check what happens in the arr access, but I am sure each access will be check string bounds anyways, so the only improvement here would be acess with null string terninator.
// to get a speed up as one would have in c++, so I am guessing depending on the back altought the v8 is back by c++, could get a mabye not get one less if check.
// typically if the input string 'str' is null terminated '\0' then this block of code wouldn't be needed.ONe see speed up of one less up front if statment, no branch prediction needed, typically only
// when get near end of the string there would be a penalty for additional loop, increment, condition, if-break. for the posibly 90% of evaluations, improve branch prediction.
// versus assignment, condition-execute.
// if (end > str.length) {
// end = str.length;
// }
for (let j = 1, k = i + 1; k < end; j++, k++) {
console.log(str[k] + ":" + strJohn[j]); // check iterations here for tetsing.
if (str[k] !== strJohn[j])
return; // break;contiunue in sequence, or continue(2), in some circumstances, however, not if want futher cod eto run that is a results of positive match in other skip blocks.
}
countJohn++;
})();
// Code that would typiclaly execute after the skip statment.
//*** */:label-skip-alternative-matching-logic ***
{
end = Math.min(i + strMary.length, str.length);
for (let j = 0, k = i; k < end; j++, k++) {
const ch = str[k];
if (ch !== strMary[j]) {
countMary--; // Many additional subtractions operations at a low level are required, can also result in greater mistakes and complicated code as assume all are matches, which for this case you can do.
break;
}
}
}
{
end = Math.min(i + strWasley.length, str.length);
for (let j = 0, k = i; k < end; j++, k++) {
const ch = str[k];
if (ch !== strWasley[j]) {
countWasley--; //unessary computation, bnut require undoing computation, wasited cycles. 2X, also requires negative variables.
break;
}
}
countWasley++; // nessary sucessull computation.
// if we get out the loop we would like to assume positive, but in this case,
// we have to countWasley
}
}
console.log("John:" + countJohn);
console.log("Mary:" + countMary);
console.log("Wasley:" + countWasley);
return countJohn === countMary && countMary === countWasley;
}
console.log(johnMary("John"));
console.log(johnMary("John&Mary&Wasley\0"));