Skip to content

Profiling - Time difference in ms#26

Open
fijiwebdesign wants to merge 5 commits into
latentflip:masterfrom
fijiwebdesign:profiling
Open

Profiling - Time difference in ms#26
fijiwebdesign wants to merge 5 commits into
latentflip:masterfrom
fijiwebdesign:profiling

Conversation

@fijiwebdesign
Copy link
Copy Markdown

This adds the time difference in millisecs between each call while preserving the file and line numbers of the caller.

Should be a start to fixing #17 and #22

The tests won't pass since they test for static values and the profiling adds dynamic time differences. It seems PhantomJS also doesn't have string interpolation in the console.log and we need that to pass Date and coerce to string.

How it works is that Date.prototype.toString() is called when the Date instance coerced into a string. This allows us to return a different scalar value each time console.log interpolates %s for our Date instance by overriding toString() locally on the instance.

Overriding instance.__proto__ works in Node and the browser. It is supported by all major browsers and IE11+ but not a spec. It will be supported for legacy ES5 in ES6 specs. See: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/proto

Currently can't think of a way to avoid using proto.

@latentflip
Copy link
Copy Markdown
Owner

latentflip commented Apr 28, 2016

Ooh, nice hack, though I think you can get rid of __proto__ by just modifying the instance:

var interval = new Date();
interval.now = (new Date()).getTime();
interval.toString = function () {
  return -this.now + (this.now = new Date().getTime())
}

log = console.log.bind(console, " +%sms ", interval);

log('A');
log('B');
log('C');

@fijiwebdesign
Copy link
Copy Markdown
Author

interval.toString. Lol, That works :)

Ideas on getting the tests to pass? maybe expose interval so it can be mocked? or use regex on the results?

@fijiwebdesign fijiwebdesign changed the title Profiling Profiling - Time difference in ms Apr 29, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants