Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Profiling - Time difference in ms #26

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

fijiwebdesign
Copy link

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
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
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