Performance wise mine maybe is worst (didn't tested it, but I think setters/getters don't perform as well as just using methods, I might be wrong), but besides performance, we always have to factor DX, and from my point of view, getters and setters when working with classes provide a better DX. I personally prefer to just use functions and not even go into classes, but still 🤣
I usually take DX quite seriously on more important things, this was me just having a bit of fun.
The only thing I'm not sure about your code is, the use of constants instead of the static fields on the class. My reasoning behind this was to be able to change them in case the user wanted to use cyrillic or some other alphabet. Although thinking about it now, it would've made more sense to pass them in the options. Any reason why using constants would be considered better DX in this scenario?
Ohh, I also tested the performance of your code, it scored second amongst the rest of the cases. So get/set did not affect significantly, so no big sacrifice there!
Accessibility First DevRel. I focus on ensuring content created, events held and company assets are as accessible as possible, for as many people as possible.
I though that too, but I did some benchmarks, and found out concat to perform a bit faster than +=. It was not the most extensive benchmark, and it might vary from browser to browser and such... but would be interesting to know though
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
A few more improvements that you could have (mainly for DX, not so much for perf) are:
#
.this.generate = this.generate.bind(this);
, so folks can use that function in stuff like maps without having issues.Applying those suggestions, the code looks something like this (I added JSDocs to have better DX as well):
Performance wise mine maybe is worst (didn't tested it, but I think setters/getters don't perform as well as just using methods, I might be wrong), but besides performance, we always have to factor DX, and from my point of view, getters and setters when working with classes provide a better DX. I personally prefer to just use functions and not even go into classes, but still 🤣
Cheers!
I usually take DX quite seriously on more important things, this was me just having a bit of fun.
The only thing I'm not sure about your code is, the use of constants instead of the static fields on the class. My reasoning behind this was to be able to change them in case the user wanted to use cyrillic or some other alphabet. Although thinking about it now, it would've made more sense to pass them in the options. Any reason why using constants would be considered better DX in this scenario?
Cheers and thanks for the additions!
My main reason is mainly usage:
Super niche, I know, but the kind of encapsulation that used to be useful from classes now I get from modules :D
Ahh okay, makes sense
Ohh, I also tested the performance of your code, it scored second amongst the rest of the cases. So get/set did not affect significantly, so no big sacrifice there!
Just wondering, is it faster to do
vs just
I would imagine as
concat
is not performant it would be faster.Only glanced at it though so I might have missed a good reason for
concat
.I though that too, but I did some benchmarks, and found out concat to perform a bit faster than
+=
. It was not the most extensive benchmark, and it might vary from browser to browser and such... but would be interesting to know though