Sunday, 15 February 2015

javascript - Best way to code a single line if statement -



javascript - Best way to code a single line if statement -

so have been writing in javascript lastly 5 years , self taught. 1 problem have come across meet more , more programmers. find not coding things best way possible.

i have been trying figure out best way programme single line if statement in js. not care readability , project not open source, not worried others adding it.

how write statements

condition = true; if (condition) runfunction(); else runsecondfunction();

but have found keeps telling me utilize brackets. have been writing off because throw in next line "it makes easier read"

condition = true; if (condition) { runfunction(); }else{ runsecondfunction(); }

and lastly said me should write if statements more closely match how written in c# looks following

condition = true; if (condition) { runfunction(); } else { runsecondfunction(); }

and of course of study there shorthand, utilize toggling variables this

condition = true; (condition) ? status = false : status = true;

but improve utilize shorthand regular if statements well. example:

condition = true; (condition) ? runfunction() : runsecondfunction(); so question is

out of of many ways write single line of javascript best way programme run fastest. not care readability, please don't recommend write way or way because easier read. please cite examples of why 1 way faster or slower another.

to clear

it not don't care readability none of if statements hard me read, create sense me in versions. of may consider hard read may not. brings me next point, since none of hard me read, maybe should write best way js handle it.

for illustration found js takes longer run

counter++;

then does

counter = counter + 1;

so clear don't have problem reading of these , know best js handle.

take @ jsperf tests:

braces vs no braces ternary vs if if vs boolean

they seem relatively equal (aside boolean version beingness slower in test ran), go whatever of best.

javascript

No comments:

Post a Comment