in reply to A comparison of Perl vs. JavaScript -- a reference table.

If you ever decide to create your JavaScript to Perl converter, here are a couple of things I learned:

If you need to work with a long string, replacing characters, inserting strings, splitting/joining strings, then in JavaScript it is a lot faster to convert the string into an array of characters and then work on the array elements one by one. The split() and join() functions are extremely fast in JavaScript, however if this code is converted into Perl, it becomes very slow! I've noticed that Perl code runs faster if you leave the string as a string and work on it by replacing characters using the vec() and substr() functions. Perl is better with strings than arrays. Having a big array in Perl just slows things down. In JavaScript, it's just the opposite!

Secondly, the % operator works quite differently in Perl than in JavaScript! Consider the following JavaScript code:

C = A % B;

This translates to the following Perl code:

$C = $A - int($A / $B) * $B;

So, this operator is tricky! It's easy to overlook this difference.

  • Comment on Re: A comparison of Perl vs. JavaScript -- a reference table.

Replies are listed 'Best First'.
Re^2: A comparison of Perl vs. JavaScript -- a reference table.
by LanX (Sage) on Jan 03, 2021 at 04:58 UTC
    Strings are immutable in JS, e.g. a regex replace there requires an assignment to a new variable.

    I.e. it's always s///r there.

    I'm not sure if I'm still up to date, but the internal text format in JS is UTF 16 while it's kind of UTF 8 in Perl. This requires 2 or 4 bytes /character in JS. So more memory needed but faster to split.

    5 % 2.2 is 1 in Perl but ~0.6 in JS. Perl accepts only the integer part of the arguments. It's documented in perlop but still surprising.

    Please note that 50%22 is 6 in both languages.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

    ) Well IFF the utf8 flag is enabled in Perl that is.