Author: Laruence
A piece of supporting evidence for event handler references
Earlier, when I was analyzing the Javascript this keyword, I said that when you write a DOM element’s event handler inline, it is applied by reference. It just so happens that a friend named nullbyte gave me a very interesting case:
<img id="foo" src="xxx" onerror=" alert('error');
} function foobar() {
alert('www.laruence.com'); " />
<script>
alert(document.getElementById("foo").onerror);
</script>
Try it in IE.. Though it doesn’t work in FF or Chrome either — presumably because both FF and Chrome validate the legality of the HTML code.
The efficiency of Javascript regex
If you see someone writing a Javascript trim using a loop instead of a regex, don’t laugh. They have experience on their side. Consider how long the following code takes to execute:
var matchs = /^(a+)+$/.exec("aaaaaaaaaaaaaaaaaaaaaaaaaaaX");
alert(matchs);
Let me tell you… Note: the results below come from eyeballing a watch, but that doesn’t affect the magnitude of the times… Also a friend named stauren from jsmore confirmed this conclusion:
IE8: 30 seconds
FF3: 28 seconds
The supposedly fastest Chrome, which uses the V8 engine: 8 seconds.
How utterly unacceptable is that result? The detailed analysis of the reason was mentioned in master regular expression.
There is a difference between NFA and DFA engines. js/perl/php/java/.net are all NFA engines. And the mechanical difference between DFA and NFA brings five consequences: 1. DFA only needs to scan each character in the text string once, which is faster, but it has fewer features; NFA chews characters back and forth, swallowing and spitting them out, so it is slow, but its features are rich, which is why it is actually more widely used — today’s major regex engines, such as Perl, Ruby, Python’s re module, and the regex libraries of Java and .NET, are all NFA. 2. Only NFA supports features like lazy and backreference; 3. NFA is eager to claim credit, so the leftmost sub-regex that matches successfully wins first, which is why it occasionally misses the best match; DFA, on the other hand, is “the longest leftmost sub-regex that matches successfully wins”. 4. NFA uses greedy quantifiers by default (that is, for patterns like /.*/ and /\w+/ that “repeat n times”, it proceeds greedily, matching as many characters as possible until it has no choice but to stop), and NFA prioritizes quantifiers. 5. NFA can fall into the trap of recursive calls and perform extremely poorly. backtracking — when NFA finds it has swallowed too much, it spits characters back one by one, looking for a match as it goes; this process is called backtracking. Because this process exists, during NFA matching, especially when writing unreasonable regex matches, the text is scanned repeatedly and the efficiency loss is not small. Understanding this is very helpful for writing efficient regular expressions.
As for regex in Javascript, it should be prioritizing quantifiers, which causes deep recursion and creates performance problems…

