JavaScript: Fast Numeric String Testing

Sometimes I have strings that (should) contain numbers (like ‘31415’) but I want/need to test them before I use them. If this happens in a loop I could start asking myself questions about performance. And if it is a long loop an a Node.js server the performance may actually matter.

For the purpose of this post I have worked with positives (1,2,3,…), and I have written code that finds the largest valid positive in an array. Lets say there are a few obvious options:

// Parse it and test it
const nv = +nc;
pos = Number.isInteger(nv) && 0 < nv;

// A regular expression
pos = /^[1-9][0-9]*$/.test(nc);

// A custom function
const strIsPositive = (x) => {
   if ( 'string' !== typeof x || '' === x ) return false;
   const min = 48; // 0
   const max = 57; // 9
   let   cc  = x.charCodeAt(0);
   if ( cc <= min || max < cc ) return false;
   for ( let i=1 ; i<x.length ; i++ ) {
     cc = x.charCodeAt(i);
     if ( cc < min || max < cc ) return false;
   }
   return true;
 }
pos = strIsPositive(nc);

Well, I wrote some benchmark code and ran it in Node.js, and there are some quite predictable findings.

It is no huge difference between the alternatives above, but there are differences (1ms for 10000 validations, on a 4th generation i5).

There is no silver bullet, the optimal solution depends on.

If all you want is validation, it is wasteful to convert (+nc). A Regular expression is faster, but you can easily beat a Regular expression with a simple loop.

If most numbers are valid, converting to number (+nc) makes more sense. It is expensive to parse invalid values to NaN.

If you are going to use the number, converting to number (+nc) makes sense (if you convert only once).

The fastest solution, both for valid and invalid numbers, is to never convert to number (but use the custom function above to validate) and find the max using string compare.

if ( strIsPositive(nc) &&
     ( max.length < nc.length ) || ( max.length === nc.length && max < nc )
   )
  max = nc; 

This is obviously not a generally good advice.

Other numeric formats

My above findings are for strings containing positives. I have tested both code that only validates, and code that use the value by comparing it.

You may not have positives but:

  • Naturals, including 0, which creates a nastier regular expression but an easier loop.
  • Integers, including negative values, which creates even nastier regular expressions.
  • Ranged integers, like [-256,255], which probably means you want to parse (+nc) right away.
  • Decimal values
  • Non standard formats (with , instead of . for decimal point, or with delimiters like spaces to improve readability)
  • Hex, scientific formats, whatever

In the end readability is usually more important than performance.

Stress testing with Raspberry Pi

I have a system – a micro service architecture platform – built on Node.js. It can run on a single computer or distributed. It is a quite small system but quite critical that it works correctly.

Under what circumstances would the system fail to work correctly? How much load can it handle? How does it behave under too heavy load?

Stress testing is difficult, and expensive. Ideally you have plenty of test clients simulating realistic usage. It can be done, but often not easily. A simple and cheap option is to run the system on less resources.

My system used to run perfectly on a Raspberry Pi. The tests work fine. I have also kept the integrationtests working (although there have been timing issues). However, the other day I tried to restore production data to the Raspberry Pi, and it failed to run properly. Problems were

  • High latency and timeouts
  • Heavy swapping
  • Escallating retries, making the situation worse

The last point is particularly interesting. Error handling is designed for stability and recovery, but it risks increasing the total load, making the system even more unstable.

I did make the system work on a RPi again, and in doing so I leant about real problems, and fixed them. It is an interesting excersise in finding problems in systems that don’t work properly, and it is a practical way to “measure first, optimize second”.

Does your system work, with a reasonable amount of production data, on a Raspberry Pi?

Old and new Glenfiddich

I was given an old Glenfiddich miniature. It is probably from the 1960s and it has been stored dark and cold.

I bought a kit with the current Glenfiddich line of standard whiskies and made a little tasting. This is what I find about the old miniature.

The color is more pale, which is reasonable given that it the label says it was 8 years old when bottled, and there is no reason it should get darker being stored in a bottle.

A few minutes in the air made the aroma more pleasant. It was still a light, fruity (almost like white wine) aroma – not very different from the 12 YO.

Both on the nose and in the mouth I found the dominant thing to be “jerusalem artichoke”. I occationally find that in whiskies, never in Glenfiddich before, but sometimes in old whiskies.

All the modern Glenfiddich are partly stored on Sherry casks. I think the miniature was not, and I think that makes it taste a bit different.

I think most people would have prefered the 15/18YO to the miniature. But it is a matter of taste. It had not turned bad. If the difference between the miniature and the 12YO is mostly because of different production methods, or 50 years on a bottle, I can not tell.

I doubt this miniature had been a major success if it was sold. Everything was not better in the past, and storing whisky for half a century on a bottle is hardly a silver bullet to fanstatic whisky. Perhaps it give a taste that is hard to find otherwhise.

Linux on Hades Canyon

About a year ago I got a Hades Canyon NUC for Windows and Gaming. I have been happy enough with it to buy another one for running Linux, Xubuntu.

Basically Xubuntu 19.10 works perfectly on the Hades Canyon NUC. It was all smooth, except:

  • HDMI audio is low quality – for me it is acceptable to use the 3.5mm plug instead, but if you want HDMI audio this is bad for you

I think with NUCs Intel has managed to produce computers that are very good, and I don’t really see myself buying any other desktop computers. Why are there no AMD computers in this segment?

Keeping open Whisky bottles

So, you open a bottle of whisky, drink a little now and then, and years later you wonder if it still tastes the same?

Here are my empiric notes:

Deanston 12: One of the bottles is opened since perhaps 2 years, and has been almost empty for months. Head to head, initially, I slightly prefer the old bottle on the nose. But tasting, going back and forth, it is the same whisky.

Famous Grouse: I have a plastic bottle of Famous Grouse with very little left in it. It has probably been open for 10 years. It actually tastes significantly worse – more burning and chemical – than a fresh bottle.

Lagavulin 1984: This bottle has been opened for almost 20 years and there is not much left. When it was newly opened I compared it to a standard Lagavulin 16. Then my experience was that 1984 and 16YO was very similar, but 1984 was a little extra. Today I opened a new Lagavulin 16 and compared to the 1984. The 1984 is much softer, I guess the ABV is lower, and both aroma and taste has a clear jerusalem artichoke element to it. My conclusion is that 1984 is definitely changed, not necessarily for the better, but it is not much worse either. It is still enjoyable (some people would probably prefer it for being softer), it is still a Lagavulin, but it is not exactly the same.

Simple Vegetable Oil Lamp

WARNING: The lamp prototypes suggested below may not be safe for general use: especially not around children, left unattended, or close to anything flamable.

Oil Lamps

I got a beautiful Oil lamp that I use much.

Oil Lamp

This lamp uses Lamp Oil (kerosene, paraffin oil). When I bought that I was a little chocked with two thing:

  1. The price (compared to vegetable oil)
  2. How seriously poisonous it is (to the point I dont like to handle it, and I wonder if I want it at home at all)

However this “real” Oil lamp does not run well on vegetable oil (I have tried canola oil). It runs for a while but I think the problem is that the viscosity is too high so the oil does not flow properly upwards through the wick as required.

Vegetable Oil

I can buy canola oil for 25% of the price of lamp oil. And it is obviously not dangerous (since it is for cooking). However it is thicker and has a higher flash point. It is also supposed to not burn cleanly (leaving smoke and smell). So I was curious if I could design a simple practical and not too ugly oil lamp for simple (unused) cooking oil.

Skipping the failed designs here are the ones that kind of work.

A can lamp

What you see in the picture are five components:

  1. a metal can
  2. canola oil
  3. a few candlewicks
  4. a metal washer (the flat metal ring with a small hole in it)
  5. a metal “bridge”

placed inside a fireplace. This burns well: no smoke, no smell, burns for hours. I have read that vegetable oil consumes the wick faster than lamp oil. Perhaps that is true, but nevertheless the wick lasts much longer than it would have in a normal candle.

A little bottle lamp

How about moving the metal washer with the wick to a small bottle?

This is a very simple design and as you can see in the (somewhat unsharp) picture it burns nicely. But it only burns nicely for about 60min, and then it burns barely for another 60 minutes and then it dies.

Only the canola oil in the bottleneck is consumed. After that it appears the height difference between the oil level and the washer/fire prevents the oil from ascending the wick (fast enough).

A used candle jar

I tried filling an old candle jar with about 1cm of canola oil, and used a wick and a metal thing for this result.

This burns nicely! The sides of the candle jar does not get very hot, and the bottom of and the oil remains quite cool. The metal thing from a hardware store is obviously designed for another use.

The good thing with this design is that it is simple (jar+metal thing+wick) and that not so much oil goes into the lamp. You can easily reuse pretty candle jars that are already designed for the purpose.

Spirit Burner

I would not guess that most spirit burners (or oil lamps) work well. But SPIRI-1 from Böhm Stirling-Technik works perfectly with canola oil. The good thing is that it is (roughly) the size of a tealight so you could replace your disposable tealights. The bad thing is that it is quite expensive.

Conclusions

First I think vegetable (canola) oil is underestimated for decorative light at home. However I can see that tealights can be sold and managed in a safe way and are easier to use.

It often requires two matches to light the canola, because the flash point is very high. However I think the high flashpoint is also good for safety.

Cheap candles and tealights are made of petroleum and they don’t necessarily burn cleanly without leaving unhealthy particles in the air. I can not guarantee that the canola oil also does not leave any particles in the air, but the oil itself is not toxic at all.

Force Vue Update ($forceUpdate)

Occationally you want to force some part of your Vue application to update. One situation is that I have a “big” web application not written in Vue, and somewhere I add a Vue component. Something in the world around it changes but it is not aware of it, so I want to force it to update.

It seems, the vm.$forceUpdate method is just updating the component itself, and children with slots. I didn’t use slots, and $forceUpdate was useless.

So, if you do myVue.$forceUpdate() without success, try:

    myVue.$children.forEach(c => c.$forceUpdate());

It might do what you want. It did for me.

QNAP TS 251+ for Container Station

After many years of running home server systems on Raspberry Pi, another one broke down and I decided I can to better, so I bought a QNAP TS 251+ with two WD 6TB Red drives (suitable for NAS).

My objective here is to mostly use the QNAP with Container Station; for running virtual machines on it.

First Impression

The TS 251+ looks profesionally black, but it is all plastic. Installation was fine but for someone with little computer experience I would imagine it a bit scary. A few things to note:

  • It restarted several times for firmware upgrades, and restarting took some time
  • There are some “I accept privacy…”-things to accept. I guess it is fine. But one reason you get your own hardware instead of running in the cloud is that you know your data is private. So if you are paranoid, read the fine print or get into the details.
  • I suggest you familiarize yourself with RAID0, RAID1 and JBOD before you start it up.
  • I suggest you read about Static Volume, Thin Volume and Thick Volume, and make up your mind, before you start it up (I think Thin makes most sense, especially for use with Container Station).
  • The Web GUI is good – very “modern” – in a way that it almost feels like a desktop computer. A bit over-engineered and messy if you ask me. There are very many features and details, and it is a bit intimidating and confusing at first.
  • Container Station is just what I want and need!
  • I find it silent and cool enough (44C reported under load)
  • It automatically started some “Raid Syncronization” that takes about 24h with my drives. Guess it is fine, but it makes me a bit nervous with something new that I hesitate to restart or reconfigure it because it is doing something low-level and important.

Container Station Problem

When I woke up in the morning it turned out my container was down. There was message from the middle of the night:

 [Container Station] Created interface "lxcbr0" with errors. Unable to start DHCP service.   

I found this strange, I could not start Container Station again, and I found other people had had this problem with no elegant solutions. I found that the problem was solved if I deleted the two virtual switches (docker0) and (lxcbr0); Container Stations creates them automatically when it starts.

I think my container may have crashed due to too little RAM in the middle of the night, and that somehow corrupted something.

Memory Upgrade

This model comes with 2GB RAM. That is quite enough, but not if you want to run Container Station conveniently. I have switched off most QNAP services, running a single LXC Container with syncthing using about 500MB of RAM, and the QNAP complains there is little available RAM (and it uses swap). So I think it is safe to say that to run Container Station or Virtualization Station, more RAM is recommended.

Officially max RAM is 8GB but there are multiple records of people saying it works with 16GB as well. It also appears that you may use just one memory module (out of two), they dont need to be installed in pairs.

So I bought 2x8GB and it seems to work perfectly:

  • Corsair DDR3L 1600MHz MACMEMORY
  • CMSA16GX3M2A1600C11

Virtualization

There are several Virtualization options with the QNAP:

  • Virtualization Station: running real virtual machines (like VMWare), emulating hardware. Just starting Virtualization Station used almost 1GB or RAM.
  • Container Station:
    • running (LXC) virtual linux machines, emulating just the kernel. This is much more light-weight, and it means the virtual machine shares disk and RAM with the main system (you do not need to allocate disk, all disk is available and shared for every virtual machine – they just live in separate folder)
    • running Docker containers
  • Linux Station: allowing the QNAP to work as a Linux Desktop.

Apart from virtualization, the QNAP also allows you to install things like WordPress, Mediawiki, MySQL and other services as packages.

Update and Problems 2020-02-29

One of my virtual container station machines had its clock out of sync. When I started investigating I could not connect to the QNAP itself. The two virtual machines were up normally. The QNAP itself was nowhere to be seen on the network. I restarted it (using the power button – I believe it shuts down properly), it came up and it wanted a firmware update, which I immediately accepted. After that it did not come up (on the network) again.

I tried to reach it on 169.254.100.100 with no success.

I finally did a “reset” (using a paperclick on the rear side of the QNAP for 3-4s when it was already on). Following the reset it immediately appeared normally on the network. Password was “admin”.

However, the virtual container station machines did not start. I had to change their network settings to NAT, and then back to Bridge, then they worked. So it seems to me the virtual switch is not quite 100%.

All seems good now, but this took quite a while to figure out and fix. I bought a QNAP to get something much more stable and reliable than my old Raspberry Pi, but this was not impressive.

Dungeon Master Inspirational Reading

This is work in progress.

I read to get inspiration for my D&D Dungeon Master hobby.

5/5 Recommended

  • Conan Chronices (short stories of varying length and quality)
  • Northlanders 1-3 (comic)
  • Untold Adventures – Dungeons & Dragons (short stories)
  • The Outsider (short story, H.P. Lovecraft)
  • Thorgal (comic)

4/5 Recommended

  • Berserk (Deluxe, 1-2, manga)
  • Kull Exile of Atlantis (short stories)
  • Lone Wolf (Project Aon)

3/5 Average

  • Crannog Saga 1-2 (Comic)
  • Fall of Gondolin
  • Legends and Myths: Knights of the round table

2/5 Not so recommended

  • Den eviga nattens riddare + Det mörka hjärtats vilja (Swedish)
  • Legends and Myths: Charlemagne
  • Vox Machina

1/5 Not recommended

  • Legends and Myths: King Arthur

Getting Ripped with Jordan Peterson

I have never been the exercising type. I walk quite much. But I don’t run, I don’t lift weights, I don’t like to get exhausted and I don’t like when it hurts.

Having passed the age of 40 I realized I am not getting younger, healthier or stronger. Although a little bit heavier – not overweight at all, but skinny-fat.

In the autumn of 2018 I listened to Jordan Peterson talking about his book 12 Rules for Life. This particular lecture was about Rule #4: Compare yourself to who you were yesterday, not who someone else is today. Peterson mentioned in his lecture that if you make small consistent improvements over a long period of time, you will eventually make significant improvements in the long run.

Inspired by this I set up a scheme of exercising that works for me:

  • I have a number of simple exercises I can do at home, in a hotel room, or elsewhere: pushups, squats, planks and things like that. Most are body-weight only exercises.
  • Every week I do at least as many of each exercise as I did last week.
  • Every week, in total, I do more than last week.

That is it!

I started out with very low ambition. The first weeks I did ridiculously little exercise. But after 8-9 weeks I saw some improvements, I had got the habit, and I did not want to fail and give up. I have now done this for a little more than a year, and not a single week I have failed to improve.

A few more things:

  • A week starts on Sunday (so it is easy to get a good start) and ends on Saturday (so I have time to catch up after a bad week)
  • Some exercises I do better/heavier after a while, even though the amount is the same. For example, there are different pushup techniques and I allow myself to change between them, generally this makes my exercise harder than it was a year ago. I get more quality out of the same time or repetitions.
  • Sometime I add a new exercise to the list rather than doing more of the old ones.

This is my weekly (every 4 week printed) progress:

Week  49    1    5    9   13   17   21   25   29   33   37   41   45   49
a)   195  320  380  400  420  435  440  450  460  460  460  460  460  460
b)                        30   30   35   50   60   65   70   75   80   80
c)    40   75  105  120  120  120  120  120  125  130  130  140  140  140
d)    29   80  100  120  155  160  165  165  171  175  175  180  180  180
e)    60   85  105  120  120  120  120  120  120  125  125  125  125  125
f)    30   45   60   75   80   90  100  100  120  120  135  150  155  170
g)                   20   30   35   40   45   55   60   65   70   70   80
h)                                  30   45   50   50   50   50   50   50
i)                                                 15   20   25   25   30
j)                                                 15   30   35   35   40
k)                                                                60   60

Total 354  605  750  855  955  990 1050 1095 1161 1215 1260 1310 1380 1415

The different exercises are here named a-k (the important thing is that you find exercises that you like) and the number can be seconds or repetitions (sometimes this is for two sides so I do twice as many). So I think it is a good guess that I exercise almost one hour per week, but that is effective time. I would not be able to do this in a gym in one hour, that would be too heavy.

As you can see it levels out a bit. It is hard, I sometimes hate it, but I improve and I do not give up!

In the beginning I set up a few goals: new personal records when it comes to pushups and planks for example. I reached those and now my goal is to be able to walk on my hands. So I do see results! Also my body looks and feels different.

Discipline, systems and motivation

Motivation will not take you too far. There comes a day when motivation simply fails you and you can lose a good habit. However, if you are disciplined about systematic improvement, you do not fail even when your motivation is low.

Conclusion

If you already exercise regularly and you are happy about it, you are probably already better than I will ever be. But if you really do not exercise and you are aware that you and your body would benefit from it, I think this is a method for you.

Start out with very low ambition. You need to negotiate with yourself (as Peterson says). Perhaps you can do 10 pushups and 10 situps the first week? And 11 the next. Do it, you have nothing to lose. And after a few months giving up on your good development is harder than doing those damn pushups.

I think for me a weekly goal has been good. Some days are just not good days but I can make my weekly goal anyway.