Opened 11 years ago
Closed 10 years ago
#27034 closed enhancement (fixed)
jQuery methods html('') vs empty()
Reported by: | madeinua | Owned by: | wonderboymusic |
---|---|---|---|
Milestone: | 4.2 | Priority: | normal |
Severity: | normal | Version: | 3.8.1 |
Component: | Administration | Keywords: | has-patch |
Focuses: | javascript, performance | Cc: |
Description (last modified by )
I found that in a many places in WP uses .html('')
instead of .empty()
There is a few advantages to use the second one:
$.empty()
works faster$.html('')
creates memory leak$.empty()
is more readable
Confirmation of these words you can find on the internet (e.g. stackoverflow).
Thanks!
Attachments (2)
Change History (13)
#2
@
11 years ago
- Keywords has-patch added
- Milestone changed from Awaiting Review to 3.9
- Type changed from feature request to enhancement
#5
@
11 years ago
- Milestone changed from 3.9 to Future Release
In order to touch this we'd have to ensure there are no side effects for any one of these changes.
#6
@
10 years ago
Removing events is only 'dangerous' if removed elements are going to be returned to the dom.
From my inspection, this does not happen anywhere. The only place where object is returned (and where events could potentially break) when .html('')
is used is in wp-ajax-response.js, so just to be sure, i used detach()
there instead.
#8
follow-up:
↓ 9
@
10 years ago
We may not be set up to handle this right now, but just a note for now/the future that it would be nice if this was testable in some way, particularly in terms of performance.
#9
in reply to:
↑ 8
@
10 years ago
Replying to helen:
We may not be set up to handle this right now, but just a note for now/the future that it would be nice if this was testable in some way, particularly in terms of performance.
Here is some jsPerf data comparing speed for empty(), html() and native .innerHTML =
According to jQuery documentation,
empty()
also removes event handlers from child elements, so we should verify that doesn't affect any of the instances here.