Skip to content

Commit

Permalink
JSON: Fix property and string patterns performance. Fix #1080
Browse files Browse the repository at this point in the history
  • Loading branch information
Golmote committed Jan 10, 2017
1 parent 23c6e8a commit 0ca1353
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
14 changes: 7 additions & 7 deletions components/prism-json.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Prism.languages.json = {
'property': /"(?:\\.|[^|"])*"(?=\s*:)/ig,
'string': /"(?!:)(?:\\.|[^|"])*"(?!:)/g,
'number': /\b-?(0x[\dA-Fa-f]+|\d*\.?\d+([Ee][+-]?\d+)?)\b/g,
'punctuation': /[{}[\]);,]/g,
'operator': /:/g,
'boolean': /\b(true|false)\b/gi,
'null': /\bnull\b/gi
'property': /"(?:\\.|[^\\"])*"(?=\s*:)/ig,
'string': /"(?!:)(?:\\.|[^\\"])*"(?!:)/g,
'number': /\b-?(0x[\dA-Fa-f]+|\d*\.?\d+([Ee][+-]?\d+)?)\b/g,
'punctuation': /[{}[\]);,]/g,
'operator': /:/g,
'boolean': /\b(true|false)\b/gi,
'null': /\bnull\b/gi
};

Prism.languages.jsonp = Prism.languages.json;
2 changes: 1 addition & 1 deletion components/prism-json.min.js

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

4 comments on commit 0ca1353

@mAAdhaTTah
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Golmote Because I'm terrible at Regex, I was wondering if you could shed some light as to what this change does and why it resolved the issue in the linked ticket. Thanks!

@Golmote
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mAAdhaTTah I'm not even sure I can explain. I think the choices in the alternative should try to exclude each other, which was not the case here, because of (what I assume was) a typo. The \ could be matched by either part of the alternative, thus increasing drastically the number of possibilities. I guess that was leading to a lot of backtracking, which is just terrible for performance.

@Golmote
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mAAdhaTTah I remember we encountered similar performance issues in C-like and JS strings, some time ago. And you'll notice the fix actually followed the same reasoning: 476cbf4

@RickyRoller
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Golmote Any chance we could get this in a release? I'd love to get it pulled into our project

Please sign in to comment.