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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.