Skip to content

Commit

Permalink
Improve performance by doing more work in the worker
Browse files Browse the repository at this point in the history
Currently the worker tokenizes the code and then turns it into a
JSON string. This JSON string is sent back to the main thread as a
message and parsed. Then it is turned into a HTML string and
stored in the attribute env.highlightedCode.

These conversions into JSON are unnecessary, because we can create
the HTML string directly in the worker and send it as a message to
the main thread.

Signed-off-by: Andreas Rohner <andreas.rohner@gmx.net>
  • Loading branch information
zeitgeist87 committed Sep 23, 2015
1 parent e62c88e commit 09be99e
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions components/prism-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ var _ = _self.Prism = {
var worker = new Worker(_.filename);

worker.onmessage = function(evt) {
env.highlightedCode = Token.stringify(JSON.parse(evt.data), language);
env.highlightedCode = evt.data;

_.hooks.run('before-insert', env);

Expand Down Expand Up @@ -398,7 +398,7 @@ if (!_self.document) {
code = message.code,
immediateClose = message.immediateClose;

_self.postMessage(JSON.stringify(_.util.encode(_.tokenize(code, _.languages[lang]))));
_self.postMessage(_.highlight(code, _.languages[lang], lang));
if (immediateClose) {
_self.close();
}
Expand Down
2 changes: 1 addition & 1 deletion components/prism-core.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ var _ = _self.Prism = {
var worker = new Worker(_.filename);

worker.onmessage = function(evt) {
env.highlightedCode = Token.stringify(JSON.parse(evt.data), language);
env.highlightedCode = evt.data;

_.hooks.run('before-insert', env);

Expand Down Expand Up @@ -403,7 +403,7 @@ if (!_self.document) {
code = message.code,
immediateClose = message.immediateClose;

_self.postMessage(JSON.stringify(_.util.encode(_.tokenize(code, _.languages[lang]))));
_self.postMessage(_.highlight(code, _.languages[lang], lang));
if (immediateClose) {
_self.close();
}
Expand Down

0 comments on commit 09be99e

Please sign in to comment.