4 ms·
For the DOM element, it seems like you could just create the element once and then keep it around for reuse. Should be safe since JavaScript is single-threaded.
by exogen 14y ago
For the DOM element, it seems like you could just create the element once and then keep it around for reuse. Should be safe since JavaScript is single-threaded.
- zachrose 14y agoMy understanding is that modifying and querying DOM elements is what's slow, not just creating them. http://jsperf.com/lazy-url-parsing http://jsperf.com/lazy-url-parsing
- exogen 14y agoI'm actually surprised it's only ~40% slower (in my Chrome anyway). Considering how much less code it is to maintain, that's totally worth it IMO. I'm sure there are a thousand other functions you'd want to optimize in a real app before this.
- yahelc 14y agoAdded a test case for not caching the DOM element: http://jsperf.com/lazy-url-parsing/2 http://jsperf.com/lazy-url-parsing/2
- yahelc 14y agoClever! Didn't realize you could reuse the element.