I’m writing a function add_event as the following shows:
function add_event(o, event_type, callback, capture){
o = typeof o === "string" ? document.getElementById(o) : o;
if(document.addEventListener){
add_event = function(o, event_type, callback, capture){
o = typeof o === "string" ? document.getElementById(o) : o;
o.addEventListener(event_type, callback, capture);
}
}else if(document.attachEvent){
add_event = function(o, event_type, callback, capture){
o = typeof o === "string" ? document.getElementById(o) : o;
o.attachEvent("on" + event_type, callback);
}
}
add_event(o, event_type, callback, capture);
}
Now my question is whether the statement
o = typeof o === "string" ? document.getElementById(o) : o;
affects the performance of this program? If you pass an HTML element instead of an id, you infact executes the statement o = o, this is the reason why I ask this question. Great Thanks.
Chances are that the js interpreter will optimize it out. Even if it doesn’t, the effect won’t be noticeable. And if you’re really concerned, change it to: