SYNAIKIDO
SYNAIKIDO

Reputation: 79

Is element.remove() or element.parentNode.removeChild(element) faster?

Is element.remove() or element.parentNode.removeChild(element) faster?

As the remove method for an HTML element is now widely supported (https://caniuse.com/?search=remove), I'd really like to know if there is a performance difference between element.remove() and element.parentNode.removeChild(element).

Is there any reason not to use element.remove() except from IE <= 11 support?

Thanks in advance! :-)

Best regards

Upvotes: 1

Views: 299

Answers (1)

Julien Ripet
Julien Ripet

Reputation: 558

Unless you delete thousands of elements, I don't think it will really matter.

But I didn't even know element.remove() was a thing until today, and was curious as well, so I made this Codepen https://codepen.io/shoguna/pen/jOVromp to test it a bit.

From my tests, I find a small performance boost using element.remove() on Firefox Dev Edition Version 86.0b7 (64-bit), but that's over 100 000 elements, and could just be due to an error in my testing methodology.

I leave to you the testing in the navigator of your choice, if you want to tinker around with the codepen.

Cheers

Upvotes: 1

Related Questions