The current process, used by every (top 3 at least) syntax highlighters:
Step 2 is an awkward mechanism which triggers layout reflow1 in
the main thread, hence contributing to “jank” and First-Meaningful Paint
delays. Additionally, it enlarges the DOM further exacerbating the issue.
This prototype attempts an alternative method using position offsets,
so it (should) avoid these issues altogether.
Simply put: it’s faster.
[{ token: 'keyword', start: 5, end: 10 }]
forEach
is colored half
for for
and the rest separate.tests require Node
v22+
node --run test
serve demo sandbox locally
$ node --run demo
MIT License
Copyright (c) 2024 Nicholas Kyriakides
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and documentation files (the “Software”), to
deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
this is distinct from forced synchronous layout reflow, the reflow that is triggered by accessing certain geometric APIs. Both are types of reflow. ↩