Improve performance by using for-of loop instead of iterate functions #79
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Use
for-of
loop instead of iterate functions likeArray.forEach()
orArray.reduce()
. It receives better performance and readability of code.Motivations
Marpit internal plugins traverse the parsed tokens from Markdown many times. We were always used JavaScript iterate functions because of restricted by ESLint rule from Airbnb. However,
for-of
statement has better performance.Marpit has targeted to modern environments. (Node >= 6 LTS, Modern browser) Airbnb says that its polyfill (regenerator-runtime) is too heavyweight, but our target environments are not required polyfill. Thus, we have allowed
for-of
by overriddenno-restricted-syntax
ESLint rule .One more motivation is the readability of code.
Array.reduce()
had well used to rearrenge tokens, but they have not-intuitive codes. UsingArray.push()
to the empty array looks like better thanArray.reduce()
.It is not means that the all of iterate function are disallowed. We're still using iterate funcs to the place that has less impact of performance and expects to lost code readabilty by replacing to
for-of
.Benchmark
We have measured the time of 1000 conversions from example of uncover theme. Averages of 20 trials are below: