in reply to OT: Converting some js to Perl

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.

Replies are listed 'Best First'.
Re^2: OT: Converting some js to Perl
by BrowserUk (Patriarch) on Feb 27, 2018 at 22:41 UTC

    The entire question was one of whether that particular construct -- a library call wrapped in an anonymous function at the call site -- had any subtle side effects that were not obvious to me as a programmer with limited js experience. The upshot is that the js is generated code (GWT) and the anonymous function wrapped call to index() is probably an isFound() or doesContain() method call, that get optimised down by the Chrome js engine.

    As for the Perl; the part of the js code -- a fairly obscure algorithm -- I was interested in, was brute force extracted to perl; then refactored to suit my needs, in my favorite and most natural programing language, then that was translated again into yet a third language; so the performance of the Perl code wasn't important; ensuring that I understood the subtleties of the somewhat obfuscated js code was.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
    In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit
Re^2: OT: Converting some js to Perl
by Your Mother (Archbishop) on Feb 27, 2018 at 19:05 UTC
    I did a benchmark test one time.

    For JS in particular this would have to be repeated somewhat often on multiple platforms to remain current knowledge since major features keeping rolling in and the engines get tweaked and completely swapped out now and then.