The index() function always returns -1 or greater, so I think, we could further simplify the perl code as such:
return $Mb if (index($a, $Lb) >= 0);
We could achieve the shortest code by writing:
index($a,$Lb)<0 or return $Mb;
OR if we could see the rest of the code, then write it all in one line like this:
return index($a, $Lb) < 0 ? $Xb : $Mb;
The JavaScript code, of course, could have been more clearly written as such:
if (a.indexOf(Lb) >= 0) return Mb;
There is no need to enclose the code in self-executing anonymous functions in JS as long as the code is just a few lines or uses no private variables. It just makes no sense. It just makes the program slower.
Function calls waste CPU time. I did a benchmark test one time. So, if a part of the program runs in a cycle many times, then it's best to write it in such a manner that it involves as few function calls as possible.
For example, if a sort algorithm needs to swap two elements of an array, it's best to not write a swap() function for that. Your program will run a lot faster without calling swap().
Prototype functions are the worst. If you call ARRAY.swap(index1, index2) vs swap(ARRAY, index1, index2) the first function call will be 2-3X slower than the second one. There is no difference in the output result. It's the same thing, but one makes your code extremely inefficient. I know, I am way out on a limb here, but just wanted to let you know. Speed matters! You know, users don't care about how the code looks under the hood. All they care about is the speed of the website.
In reply to Re: OT: Converting some js to Perl
by Anonymous Monk
in thread OT: Converting some js to Perl
by BrowserUk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |