Nnadozie Okeke
dozie.dev

Follow

dozie.dev

Follow

Using textContent vs innerHTML

Nnadozie Okeke's photo
Nnadozie Okeke
ยทAug 10, 2019ยท

1 min read

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