2

The JSON string I want to convert contains date values of the format value = "YYYY-MM-DDTHH:MM:SSZ" but for some technical reasons I won't digress into new Date(value) won't work. However, a value of the format "YYYY/MM/DD HH:MM:SS" will work.

Also, using another method, breaking value down into year, month, date, hour, minute, second and creating a new Date() with those values also works.

The seemingly longer approach, to break the value down into parts looks like this:

var ymd = value.split('T')[0].split('-');
var hms = value.split('T')[1].substr(0,8).split(':');
return new Date(ymd[0], ymd[1]-1, ymd[2], hms[0], hms[1], hms[2]);

The one-liner, simpler looking approach is this:

return new Date(value.replace(/-/g, '/').replace('T', ' ').substr(0,19));

Both work, but the first one, which looks more complicated, is noticeably faster than the one-liner. I have an Array with some hundreds of elements, each containing an Object having several date values in the original string format. With the first, longer-looking method everything returns basically instantly. With the second one-liner approach there is a noticeable pause of a second or two with each return.

I'm just curious why the first approach is so much faster than the second approach. Any ideas?

Thanks.

Dexygen
  • 12,287
  • 13
  • 80
  • 147
Doug Lerner
  • 1,383
  • 2
  • 17
  • 36
  • 1
    "noticeably faster" How did you measure this? – Dexygen Jan 23 '19 at 13:04
  • 2
    Both the `replace`s in the second likely need to iterate the entire string, whereas the first method splits the string, then deals with smaller pieces. – Carcigenicate Jan 23 '19 at 13:06
  • You could make the "longer" approach longer and probably faster by doing the `value.split('T')` once. – crashmstr Jan 23 '19 at 13:29
  • So, a simple split is faster then running a RegEx parser with a state machine? How can that be? – Oliver Jan 23 '19 at 13:30
  • By "noticeably faster" I based it on how long it took to load a page of dynamically-created HTML that depends first on the JSON to Object processing to finish first. While I didn't use a timer, the first method is essentially instanteous. The second method let's you count, "one-and-one-thousand" before it loads. I went back and forth between the methods several times and it was 100% reproducible. – Doug Lerner Jan 23 '19 at 13:35
  • A final "sum up." It hadn't originally occurred to me that the thing taking time was the replace method. I thought it might be the splitting. But the consensus seems to be that the replace takes more time. I also compared string-to-date and parameters-to-date to see which was faster: `new Date("2019/01/20 23:22:39")` vs `new Date(2019, 0, 20, 23, 22, 39)`, running them each 100,000 times in a loop. The string-to-date took 3098 ms and the parameters-to-date took 3727 ms. I was also surprised by that because I thought the string probably needed extra parsing. But the string was the fastest. – Doug Lerner Jan 24 '19 at 07:05

2 Answers2

2

According to https://medium.com/dailyjs/js-regexp-fast-and-slow-d29d6b77b06

String.split() can be 40x times faster than Regex unless certain conditions are met that result in Regex calling string.split() internally.

Reza
  • 847
  • 6
  • 14
1

First, anything related to searching strings is slower (anything without an index).

The replace needs to search any match in the string to do the replacement. The replace itself is a cpu consumer too, it needs to create a new string in the memory to manage the replace.

Another thing to considerate is the Browser engine, some will run faster than others, depends on whether the browser will use an internal function or not

See this link for more informations: Why is String.replace() with lambda slower than a while-loop repeatedly calling RegExp.exec()?

William Borgo
  • 394
  • 2
  • 11
  • 1
    The consensus seems to be that the cause is the replace. I thought it might be the `new Date()` parameter being a date string rather than the 6 date parameters. If a date string itself doesn't slow down processing, maybe I can modify the JSON itself so it is in the `YYYY/MM/DD HH:MM:SS` format and avoid the split as well. – Doug Lerner Jan 23 '19 at 22:47