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

[Performance] Use the Date.toISOString() native function if it exists #1750 #1751

Merged
merged 1 commit into from
Jan 31, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions shared/AppInsightsCore/src/JavaScriptSDK/HelperFuncs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const strAttachEvent = "attachEvent";
const strAddEventHelper = "addEventListener";
const strDetachEvent = "detachEvent";
const strRemoveEventListener = "removeEventListener";
const strToISOString = "toISOString";

const _objDefineProperty = ObjDefineProperty;
const _objFreeze = ObjClass["freeze"];
Expand Down Expand Up @@ -271,24 +272,31 @@ export function isSymbol(value: any): boolean {
* Convert a date to I.S.O. format in IE8
*/
export function toISOString(date: Date) {
if (isDate(date)) {
const pad = (num: number) => {
let r = String(num);
if (r.length === 1) {
r = "0" + r;
if (date) {
if (date[strToISOString]) {
// For Performance try and use the native instance, using string lookup of the function to easily pass the ES3 build checks and minification
return date[strToISOString]();
}

if (isDate(date)) {
const pad = (num: number) => {
let r = String(num);
if (r.length === 1) {
r = "0" + r;
}

return r;
}

return r;
return date.getUTCFullYear()
+ "-" + pad(date.getUTCMonth() + 1)
+ "-" + pad(date.getUTCDate())
+ "T" + pad(date.getUTCHours())
+ ":" + pad(date.getUTCMinutes())
+ ":" + pad(date.getUTCSeconds())
+ "." + String((date.getUTCMilliseconds() / 1000).toFixed(3)).slice(2, 5)
+ "Z";
}

return date.getUTCFullYear()
+ "-" + pad(date.getUTCMonth() + 1)
+ "-" + pad(date.getUTCDate())
+ "T" + pad(date.getUTCHours())
+ ":" + pad(date.getUTCMinutes())
+ ":" + pad(date.getUTCSeconds())
+ "." + String((date.getUTCMilliseconds() / 1000).toFixed(3)).slice(2, 5)
+ "Z";
}
}

Expand Down