Nnadozie Okeke
dozie.dev

dozie.dev

Using textContent vs innerHTML

Nnadozie Okeke's photo
Nnadozie Okeke
·Aug 10, 2019·

1 min read

Subscribe to my newsletter and never miss my upcoming articles

While working through challenge two of Facebook's and Andela's DevC qualification test, I noticed the test cases weren't accepting innerHTML over textContent, so I read up a bit. Here's why:

Although both can be used to read and write text inside an element, innerHTML can be much slower because its value is parsed as HTML. Not only that but it's also susceptible to XSS attacks. Read more here.

Given all that was needed was writing some text into a span element I understand their limitations now. Especially after running a performance test of the two scenarios showed that using innerHTML in that situation is about 60% slower. You can run the test yourself here.

 
Share this