Do conditional checks cause bottlenecks in Javascript?

37 Views Asked by At

I do a lot of Javascript programming that checks for the presence of something (library, window, socket) before executing code.

if (foo()) {
  do this thing
}

Sometimes foo() gets set only once, but then I will check it over and over again during execution. React's useRef() is an good example.

const setRef = useRef(null)

setSomeRef = (someCondition)

if (someRef.current) { // someRef is defined and available
  // repeatedly execute some function 
}

My question: is repeatedly checking for the same, unchanging variable to be set during repeated execution a bottleneck? Can it be avoided? In the particular case of JavaScript, does memorization or some similar process make the check faster?

I get that this is a common thing to do (while(true) being example #1) but I'm interested in 1) the effect of these checks on program efficiency -- especially as relates to JavaScript and 2) alternative approaches.

0

There are 0 best solutions below