A switch statement is probably a decent option here, yeah. You trade off a little bit of memory for what might be a few more instructions executing the switch statement, unless the compiler picks up on it and optimizes it. Maybe check godbolt for what gets generated in practice if you really care about it.
- 0 Posts
- 3 Comments
Joined 3 years ago
Cake day: June 1st, 2023
You are not logged in. If you use a Fediverse account that is able to follow users, you can follow this user.
A few things come to mind:
- The array is probably fine. It’s not going to be particularly large and lookups are
O(1) - It’s a bit weird to me that you’re using the char pointers as indices. I would probably use actual indices and spend the miniscule amount of additional stack data to improve the clarity of the code
- You could save on some indentation by returning early instead of nesting the for-loop inside the first if-statement
- Is the call to the lookup-table really safe? Maybe checking that the token from RNA is within the bounds is the way to go?
- The only thing I would even remotely care about with regards to performance is the malloc, and that’s not that big of a deal anyway unless the length of
dnais really large. Streaming the result or overwriting the presumably already malloc’d input would be the only thing I would touch, and only if I could prove that it improves performance in practice. - (added in edit): if you can guarantee that the input is well formed, you can omit the bounds check and save some effort there.
- The array is probably fine. It’s not going to be particularly large and lookups are

You’re broadly going to have a bad time trying to force the wrong language for the job, fwiw.
You could always stop doing web frontend dev - there’s plenty of other stuff in programming to do. If you’re into UI dev, then modern native mobile dev runs on either Kotlin for Android or Swift for iOS, both of which are highly competent languages. Their modern UI toolkits are Jetpack Compose for Android and SwiftUI for iOS, both of which are reactive in the style of React, but unburdened by the shortcomings of JavaScript.