google / google/closure-compiler

Overly aggressive inlining of Date expressions

Open
#3,079 5 comments 4 reactions 0 assignees View on GitHub
internal-issue-created triage-done
Dominant language
JavaScript
Stars
7.7k
Forks
1.2k
Avg merge
2d 12h
Merged PRs (30d)
6

Description

Consider this JavaScript code:

```javascript
console.log(
(function() {
var start = new Date().getTime();
var end = new Date().getTime();
return end - start;
})()
);
```

Passing this through advanced optimizations yields

```javascript
console.log((new Date).getTime()-(new Date).getTime());
```

which has different semantics: The unoptimized version will calculate an end date after the start date, and the difference will be non-negative. The optimized version will evaluate left to right, calculating the left date first, followed by the right date, producing non-positive values.

Example, using Nashorn:

```
jjs> (new Date).getTime()-(new Date).getTime()
-3
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.