RegExp object - performance issues in some cases

Tomáš Machálek tomas.machalek at gmail.com
Sat Sep 27 07:20:40 UTC 2014


Recently I've been trying to rewrite my project to be compatible with
Nashorn (instead of older Rhino). And I've found some interesting
performance issues regarding regular expressions.

It appears that object version (RegExp) with case insensitivity enabled
performs quite bad compared to Rhino and similar variants in Nashorn (see
the code below):

// ------------- variant 1 - RegExp object, case insensitive search: (6.0
sec) ------
function srchStuff(s) {
    return RegExp('foo', 'i').exec(s);
}
for (i = 0; i < 5000; i += 1) {
    srchStuff('lorem ipsum dolor sit amet');
}

// ------ variant 2 - RegExp object, case sensitive search (0.2 sec) -----
function srchStuff(s) {
    return RegExp('foo').exec(s);
}
for (i = 0; i < 5000; i += 1) {
    srchStuff('lorem ipsum dolor sit amet');
}

// ------ variant 3 - literal form (0.1 sec) ------------------
function srchStuff(s) {
    return /foo/i.exec(s);
}
for (i = 0; i < 5000; i += 1) {
    srchStuff('lorem ipsum dolor sit amet');
}

// in Rhino, all the variants seem to take about the same time (~ 0.15sec)

I know the sample represents quite a bad code as it can be easily optimized
(pattern is static -> there is no need to instantiate RegExp again and
again). But in a real-case scenario the 'srchStuff()' can be part of some
external library function I need to call multiple times and then problem
arises.

Does anybody experienced the same problem? Or is there something I am
missing?


Thank you,
Tomas


More information about the nashorn-dev mailing list