new String replaceAll Javascript not so powerful

There’s a new javascript function in the String prototype, called replaceAll.

We all tried the native replace function, to find that it only replace the first occurrence, and we have to use a regex to really replace all, like this:
“the blue fox”.replace(/blue/g, “red”);

Now, the replaceAll does what we hope, by replacing all “string1” with “string2”, in the whole string instance, returning a copy of it.

But, in the meantime, lots of people tried to create their own replaceAll function, and one of them was to use the split / join trick. We found that it’s also very powerful when we need to manage large strings, or with a large amount of strings to convert.

So, I tried to see if I can replace all my split-join usage, with the new replaceAll.
And, the answer is NO!
Instead, I will make sure I never use the new replaceall, and I’ll keep my old custom function.

The split-join is, depending on the length of the input / find / replaceBy variables, up to 5 times faster in the tests I did.
Sometimes, it’s the same speed, but most of the time, it’s faster. And, in Chrome 85, it’s never slower.

Test code:

    <button onclick="test_replaceAll();">replaceAll</button>
    <button onclick="test_split();">split</button>
    <script>
      console.clear()
      var i = 25;
      var s = "asdf".repeat(i * 100000);
      var sFrom = "sd";
      var sTo = "z".repeat(i);

      function test_replaceAll(){
        var t0 = (new Date()).getTime();
        var t1 = s.replaceAll(sFrom, sTo);
        console.log("t1", t1.length, (new Date()).getTime() - t0);
      }

      function test_split(){
        var t0 = (new Date()).getTime();
        var t2 = s.split(sFrom).join(sTo);
        console.log("t2", t2.length, (new Date()).getTime() - t0);
      }
    </script>

Look at these results: First 5 are “replace”, last 5 are “split/join”

replaceAll: 750ms
split/join: 250ms

There’s no doubt that the split/join method is still the fastest.

But, if you need to use “regex” instead, now the replaceAll is the one to use.

Calibre is slow, not your Kindle.

I recently decided to move from Kobo to Kindle. Because I have a Prime subscription, that give free books, from their “Prime Reading” offer. Also because there was a good deal on Boxing Day, we could get it for only $60.

They call it the “All-New Kindle”, something like a new version of the previous Paper White I think, without water resistance…

To manage Kobo files, I always used the Adobe Digital Editions software, who works great, fast and easy to use. It is compatible with all e-readers in the world, except Kindle.

Then, I thought Amazon did an app like that. They did an app called Send To Kindle, works really find with pdf and some other formats, but not Epub files.

So, after a few research, you found that Calibre app, that can help you copy these files to your Kindle.

I tried it. Added my Epub to the library, and hit the “send to device” button. What? That is terribly slow, and it crashed a lot of times. I thought, like others on the internet, that the kindle device or its USB port is so so slow and that’s the reason calibre is slow.

But, the problem is not the kindle. It’s calibre!

The good way to send your Epub to kindle, using the Calibre app, is to do a 2-step process.

First, select your files, click “Convert”, and choose “Mobi” format on the top right dropdown.

Then, wait. And wait again. That process is very long.

When it’s complete, select all your files again, and now hit Send to Device button. Now, it will be very fast without crashing.

I think Calibre have lots of trouble handing conversion + usb transfert at the same time.

You can see the running jobs by clicking the “Jobs” label on the lower right corner of Calibre app.

Now, restart your Kindle device, you will see all your uploaded content appear one by one for about a minute.

That said, Calibre is an easy to use, free well done app, that help us get control over Kindle devices. That’s why all Calibre users should hit the “Support Calibre” link and donate something, to encourage that developer to continue supporting that app for a long time.

How to solve Diffie-Hellman warning on Qualys SSLLabs Test

In a previous article, I talked about how you can get a better note on Qualys SSLLabs test, by configuring CAA DNS entry.

Today, we’ll talk about another warning most of us must resolve to get the “A” Rating.

On our AWS EC2 Windows 2012 R2 server, by default, we got that security issue from SSLLabs:

This server supports weak Diffie-Hellman (DH) key exchange parameters. Grade capped to B.

Solution

That is caused by the Diffie-Hellman protocol accepted at 1024 bits.  The fix proposed by Microsoft (article) is to still accept that protocole, but only at 2048+ bits.

You need to create 1 new registry entry.  Create an empty file called df.reg, and paste that content to it:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\KeyExchangeAlgorithms\Diffie-Hellman]
"ServerMinKeyBitLength"=dword:00000800

Then, double-click on it to apply these settings, an reboot.

Run your SSL test again, that warning disappeared.