Just a general question

i don’t want to look at too much of other peoples answers until after i finished something, but i want to ask a general question:

i got something that in terms of program flow is extremely short, and it’s all very readable, but i hard coded in 4 seperate objects (w/ ~9 properties each) giving direct translations (number to string converter thing). it feels like cheating, but is it?

(btw. i thought about ways to use code to reduce the objects lengths, but it seemed less efficient in my head and a bit dumb as the principle of the code would be the same)

1 Like

Objects are commonly used as maps/dictionaries in JS. If it is feasible to do what I think you’re doing, do it, absolutely. I mean programming is entirely “cheating” from one perspective, you just try to do things as efficiently as possible. Show the code, there may be something that you’re doing that you don’t need to, or its maybe not useful to have values hardcoded or whatever. But if you have a fixed set of values that you need to look up throughout the program, you’re likely doing the most sensible thing


It really depends on what you’re hardcoding. We often use constants and lookup tables for specific values that we need in a function.
This might be something like:

const HALLOWEEN = 'October 31';

or something like

const colors = {
    red: "#FF0000",
    orange: "#FFA500",
    yellow: "#FFFF00",
    green: "#00FF00",
    blue: "#0000FF",
    purple: "#80080"

What we don’t want to do is hardcode in the solutions to problems that we should be figuring out via logic. We wouldn’t want something like

const even_odd = {
2: "even",
3: "odd",
7: "odd",
100: "even",
967: "odd"
function oddOrEven(number) {
    return even_odd[number];

so i’ve been well advised and passed that challenge, but since then in another challenge (caesars cipher) i define the whole alphabet in a simple string, and that function runs fast. the operation when clicking ‘run’ on the object mapping function has a definitive pause/slowness to it.
it got me thinking would it have been better to use an array for mapping in the previous challenge? do we worry about these things generally?

just found something i’ll append here:

You can access a property’s value in an object in O(1) time. With the structure you were using with the 2 dimensional array, it could have taken O(m*n), where m is the length of the outer array and n is the length of the inner array. Objects are much better for retrieving items quickly because of they way they are stored in memory. Arrays have to be iterated over to find specific values inside them. The only time an array can be as fast to retrieve a value in O(1) is if you already know the index of the item up front. From a memory stand point, objects do take up more than arrays.

If it’s something that interests you, you can go pretty deep into optimization techniques. (@JeremyLT does this a lot in his field of programming and loves to nerd out about it). The more you get into this, the more you learn that it’s extremely dependent upon the language and the environment that the code is running in. JavaScript has the added fun of being run in browsers that can choose to implement it in different ways.

I do think it’s really valuable to learn to thing about these things and I think it’s great that you are reviewing your work critically and using it as a starting point for further research.

Just in case you feel like you might get too caught up in this particular nitty gritty, I just want to throw in there that the small efficiency improvements of things like objects vs arrays are usually seen as secondary to the maintainability of the code. This is especially true for web development, where lag is almost always due to network communication. We don’t mind sacrificing microseconds for code that is readable, concise, and follows conventions. As in all things, it comes down to balance. Understanding the different concerns helps you become comfortable with finding that balance.

1 Like

Highly debatable. Global statements like this about performance can’t really be made. It depends upon the task at hand.

An object is basically just a hashmap. For certain problems and keys, a hashmap is the fastest way to go, but for other problems an array is faster. There is no ‘always right’ answer from a performance standpoint.

100% this. Readability and maintainability is the #1 priority. If you need to do some performance critical tricks, they should be in isolated, well documented portions of your code.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.