jorkin / sprintf

Automatically exported from code.google.com/p/sprintf

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Performance improvement - instantiating identical RegExp instances

GoogleCodeExporter opened this issue · comments

Hi, first off great job with sprintf, it's awesome and I've been using it for 
years.

I care about performance, milliseconds matter, especially when people insist on 
using extremely slow browsers. I am increasing my usage of sprintf (it is the 
cornerstone of our new i18n functionality) and need to squeeze every bit of 
performace out of it that I can.

I have made some changes which improve the speed by over 10%. Since sprintf is 
already nice and fast this does not amount to a huge amount, however I thought 
I should offer you the changes and you can use them or not as you wish. I am 
particularly focused on speeding up Internet Explorer, particularly IE8, 
because it the top browser used by our clients AND its javascript execution is 
painfully slow.

The primary boost to performance comes by not creating new identical instances 
of RegExp every time a function is called.

I have commented all the changes in the attached file with my reasoning for the 
change.

Here are the results of my tests in IE8, running the code below (averaged over 
several runs):

while(i++ < 10000)
{
    sprintf("hello %d", i);
}

sprintf current version: 385ms
sprintf optimized: 335ms
13% faster

And this test (which bypasses the format cache):
while(i++ < 5000)
{
    sprintf(i + " hello %d", i);
}

sprintf current version: 759ms
sprintf optimized: 606ms
20% faster

I've attached my test file so you can see how I came to these figures. I was 
using IE8.

Original issue reported on code.google.com by ricksbro...@gmail.com on 23 Nov 2011 at 6:19

Attachments:

Fixed in https://github.com/alexei/sprintf.js/issues/6

Original comment by alexaho...@gmail.com on 1 May 2014 at 2:12

  • Changed state: Fixed