Hey! I'm YCMJason, a Software Engineer in London 👨💻. Love diving into tech puzzles and sharing them! 🧩
All views expressed here are my own opinions, so please take them with a pinch of salt! 🧂
Seems like deciding to be 20% to 70% slower for readability is the bigger anti-pattern.
//In a libraryfunctionmakeAnIndexOn(from){returnfunction(acc,current){acc[current[from]]=currentreturnacc}}constindex=people.reduce(makeAnIndexOn("dna"))
Hey! I'm YCMJason, a Software Engineer in London 👨💻. Love diving into tech puzzles and sharing them! 🧩
All views expressed here are my own opinions, so please take them with a pinch of salt! 🧂
it is not 70% slower. The first example is just 15% slower. The second example you gave, building a Map is different from building an object. So it is not a fair test. If you remove the Map test case, using spread and map is just 15% slower. jsperf.com/create-index-using-from...
I always favour readability over performance unless performance becomes an issue. Even if it was 70% slower, unless it actually shows noticeable impact on the application, I'd say it is not worth sacrificing readability. Less readable code are less optimisable because it becomes harder for readers to understand what the code is doing.
Having said that, these are all opinions. Performance vs readability seems to have been an everlasting war. I think it comes down to what are you trying to achieve with you code.
Please ignore the following, just taking the piss 😂
If speed is all you care about and you do not need to care about maintenance / readability, then go ahead and wrap everything in one big for-loop / while-loop, construct no function because functions take up memories and invoking function have their overhead. Ah perhaps you can consider writing webasm too. It is probably the fastest.
So in my first example of making an index, I used a map to create a multi-key array look up and it was 70% slower. Then realised DNA was probably "unique" HAHA. So yeah 70% faster if there is repetition.
In the map version: your "Do" is still 20% slower than the object version and lets face it, not that much more readable. The Map version is way faster of course.
I can and do write highly readable code that is fast. I wrap things in well-named functions that work efficiently :). Yes, those functions will often hide away performance, the same way as those core native functions do.
I just care about UX and UX tries not to be janky etc.
Hey! I'm YCMJason, a Software Engineer in London 👨💻. Love diving into tech puzzles and sharing them! 🧩
All views expressed here are my own opinions, so please take them with a pinch of salt! 🧂
Readability vs performance is a hard thing to argue. Everyone has their own interpretation as of, what is readability, what is performance and why one is more important than the other etc.
I hope you have read the article I linked in the beginning. .reduce is a very vey powerful function. Simply by using it incurs readability cost. Because the reader need to figure out what it does. As oppose to using Object.fromEntries which the user knows immediately that it is constructing an object. There is nothing to "read into" to understand what it does.
I won't try to convince you because I don't think I can. But please go and try writing code this way. Maybe you will gain some new insights.
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.
this post is not about space / time performance. do-while / for-loop would probably be the best if you wish to talk about speed / storage.
please check out the links attached at the beginning of the post. they explain why reduce is to be avoided.
Seems like deciding to be 20% to 70% slower for readability is the bigger anti-pattern.
Surely this is fast and readable
I disagree.
Map
is different from building an object. So it is not a fair test. If you remove theMap
test case, using spread and map is just 15% slower. jsperf.com/create-index-using-from...Having said that, these are all opinions. Performance vs readability seems to have been an everlasting war. I think it comes down to what are you trying to achieve with you code.
Please ignore the following, just taking the piss 😂
If speed is all you care about and you do not need to care about maintenance / readability, then go ahead and wrap everything in one big for-loop / while-loop, construct no function because functions take up memories and invoking function have their overhead. Ah perhaps you can consider writing webasm too. It is probably the fastest.
So in my first example of making an index, I used a map to create a multi-key array look up and it was 70% slower. Then realised DNA was probably "unique" HAHA. So yeah 70% faster if there is repetition.
In the map version: your "Do" is still 20% slower than the object version and lets face it, not that much more readable. The Map version is way faster of course.
I can and do write highly readable code that is fast. I wrap things in well-named functions that work efficiently :). Yes, those functions will often hide away performance, the same way as those core native functions do.
I just care about UX and UX tries not to be janky etc.
Readability vs performance is a hard thing to argue. Everyone has their own interpretation as of, what is readability, what is performance and why one is more important than the other etc.
I hope you have read the article I linked in the beginning.
.reduce
is a very vey powerful function. Simply by using it incurs readability cost. Because the reader need to figure out what it does. As oppose to usingObject.fromEntries
which the user knows immediately that it is constructing an object. There is nothing to "read into" to understand what it does.I won't try to convince you because I don't think I can. But please go and try writing code this way. Maybe you will gain some new insights.