简体   繁体   中英

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

  • Synaikido

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM