-
Notifications
You must be signed in to change notification settings - Fork 152
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
ISOString with static-functions as alternative to CivilObjects #53
Comments
Here are some cons to add to your list.
Parsing and formatting are the most expensive operations in moment.js. I don't think requiring every operation to incur these costs is efficient.
All of the following are valid ISO 8601 dates.
All of the following are valid ISO 8601 date times.
Most of your example code would not work correctly with these valid ISO 8601 strings. |
|
Prior art. https://docs.python.org/2/library/datetime.html I think the confusion comes from thinking of clock time as a subset of calendar time which actually isn't the case. Clock time, and calendar time are separate unit systems that only happen to be appended to one, or the other because they measure the same thing. Apparently not everyone feels that getting the date alone is easy even when there is a method. https://stackoverflow.com/search?q=get+date+only There's no way to seek for questions about confusion with constructor names in stackoverflow because the topic is relatively subjective. |
I tend to agree, but devil's advocate: Since this will be built-in to the engines, the semantics may look like that, but what engines might actually do (from JS side) is create a pseudo-string object which is passed around, but on which properties that are less expensive to manipulate can be manipulated. Only when creating this object initially and when a string is actually needed will parse/format need to be done. |
here's test-code and benchmark using Date.isoStringInstantGetMilliseconds to parse a string and output a string, on my 1.2 GHz macbook (~1 million ops / second). and as @dilijev pointed out, engines can internally represent the string as a struct with a toString() view. result: /*jslint bitwise: true, node: true*/
'use strict';
var ii, resultList, timeElapsed;
Date.isoStringInstantGetMilliseconds = function (isoDateTime) {
/*
* this function will return the
* integer value representing the number of milliseconds elapsed
* from 1970-01-01 00:00:00.000 UTC, without regarding leap seconds
*/
var milliseconds;
milliseconds = new Date(isoDateTime);
milliseconds = milliseconds.getTime() - 60 * 1000 * milliseconds.getTimezoneOffset();
return milliseconds;
};
resultList = new Array(0x10);
timeElapsed = Date.now();
for (ii = 0; ii < 1000000; ii += 1) {
// parse a string
resultList[ii & 0xf] = Date.isoStringInstantGetMilliseconds('2017-11-21T00:00:00.' + (ii & 0xff));
}
timeElapsed = Date.now() - timeElapsed;
console.error('resultList = ' + JSON.stringify(resultList, null, 4));
console.error(timeElapsed + ' ms to do ' + ii +
' Date.isoStringInstantGetMilliseconds operations parsing string');
timeElapsed = Date.now();
for (ii = 0; ii < 1000000; ii += 1) {
// parse a string and output a string
resultList[ii & 0xf] = new Date(
Date.isoStringInstantGetMilliseconds('2017-11-21T00:00:00.' + (ii & 0xff))
).toISOString();
}
timeElapsed = Date.now() - timeElapsed;
console.error('resultList = ' + JSON.stringify(resultList, null, 4));
console.error(timeElapsed + ' ms to do ' + ii +
' Date.isoStringInstantGetMilliseconds operations parsing string and converting back to string');
/*
output:
resultList = [
1511222400480,
1511222400490,
1511222400500,
1511222400510,
1511222400520,
1511222400530,
1511222400540,
1511222400550,
1511222400560,
1511222400570,
1511222400580,
1511222400590,
1511222400600,
1511222400610,
1511222400620,
1511222400630
]
469 ms to do 1000000 Date.isoStringInstantGetMilliseconds operations parsing string
resultList = [
"2017-11-21T00:00:00.480Z",
"2017-11-21T00:00:00.490Z",
"2017-11-21T00:00:00.500Z",
"2017-11-21T00:00:00.510Z",
"2017-11-21T00:00:00.520Z",
"2017-11-21T00:00:00.530Z",
"2017-11-21T00:00:00.540Z",
"2017-11-21T00:00:00.550Z",
"2017-11-21T00:00:00.560Z",
"2017-11-21T00:00:00.570Z",
"2017-11-21T00:00:00.580Z",
"2017-11-21T00:00:00.590Z",
"2017-11-21T00:00:00.600Z",
"2017-11-21T00:00:00.610Z",
"2017-11-21T00:00:00.620Z",
"2017-11-21T00:00:00.630Z"
]
1130 ms to do 1000000 Date.isoStringInstantGetMilliseconds operations parsing string and converting back to string
*/ |
Performance benchmarks are interesting, but a million operations taking 0.4 vs 1.2 ms, when 99% of use cases will be doing < 10 operations in a given second, isn't really significant. What's far more important than microbenchmarks is clarity, usability, and what's idiomatic for JS. |
idiomatic for whom? people who primarily write javascript? javascript should adopt solutions best-suited for javascript use-cases, which means datatypes (string in this case) that are more JSON-friendly and baton-passable across systems that recognize JSON. this is a unique criteria not placed highly in c#/c++/java, and why their idiomatic solutions are not always ideal for js. |
"Idiomatic" is indeed subjective - but I primarily write JavaScript (and have never written C#, and very little C++ or Java), and I find the current proposal far, far more idiomatic than your suggestion of stringly typed static methods. As far as JSON-friendly, any date format you choose in your app can be used for that. |
i proposed extending Date with 5 static functions to cover all the constructors and methods for CivilDate, CivilDateTime, CivilTime, Instant, and ZonedInstant. how is that large quantity? |
Fair; I'll edit that part out. |
I tend to think that |
and replace it with what? CivilDate, CivilDateTime, CivilTime, Instant, and ZonedInstant? do you honestly think its easier to debug-and-fix code that has to context-switch between 5 confusing temporal-types, instead of code that just has to deal with a single Date-type (and its serialized canonical-ISOString/JSON representation)? because its not. and you will be hard-pressed to come up with a tc39-proposal that will be easier-to-use/JSON-serialize than existing Date. |
creating a separate issue from sub-topic that was discussed in #51
we mentioned extending Date with static-functions to manipulate string-representations of date/time/datetime (possibly following the https://www.w3.org/TR/NOTE-datetime standard) as alternative to CivilObjects.
pros:
cons:
the static-function equivalents for CivilObject methods could be as follow:
The text was updated successfully, but these errors were encountered: