2
let number = new Date().getTime()
// number is 1523546797869
// binary: 10110001010111010011101110011011100101101
// (41 bits)

When I save it through GraphQL, I got an error which says it can only handle 32 bit because of Javascript language limitation.

In field "invoiceDate": Expected type "Int", found 1523546797869: Int cannot represent non 32-bit signed integer value: 1523546797869

My question is that if Javascript language is limited to 32 bit integer, why getTime() return a number that 41 bits??

I also read this thread. I think it is a little related, but can't fully understand the precision thing.

Nicolas S.Xu
  • 13,794
  • 31
  • 84
  • 129
  • Where does it say JS is limited to 32 bits? – tkausl Apr 12 '18 at 15:41
  • @tkausl https://github.com/facebook/graphql/issues/73 Here is the quote: "It wasn't included because it's not available across all platforms, most importantly and practically in our case: JavaScript." – Nicolas S.Xu Apr 12 '18 at 15:44
  • It says JS doesn't support 64bit integers but it doesn't say JS is limited to 32bit integers. Read your [first link](https://github.com/stems/graphql-bigint) again. – tkausl Apr 12 '18 at 15:46
  • I did read it, but still not clear. WHy JS does not support, but not limit to?? My impression if sth is not supported, the behavior is not defined. Not defined is not good at all in CS, right? We should not use it. – Nicolas S.Xu Apr 12 '18 at 15:52
  • 1
    `In JavaScript, all numbers are implemented in double-precision 64-bit binary format IEEE 754 (i.e. a number between -(2^53 -1) and 2^53 -1). There is no specific type for integers.` - Source: **[MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Numbers_and_dates)**. Tip: when searching for anything javascript, add `MDN` as a search word. – Peter B Apr 12 '18 at 15:58

1 Answers1

2

You've answered your own question with the links you have provided.

This link: Does JavaScript support 64-bit integers?. Which explains that javascript is limited to 53 bits due to it's support of IEEE-754 double-precision (64 bit) format.

And this link: GraphQL BigInt Which explains the existence of that package because GraphQl only supports 32 bit integers

The GraphQL spec limits its Int type to 32-bits. Maybe you've seen this error before:

GraphQLError: Argument "num" has invalid value 9007199254740990.
          Expected type "Int", found 9007199254740990.

Why? 64-bits would be too large for JavaScript's 53-bit limit. According to Lee Byron, a 52-bit integer spec would have been "too weird" see this issue. The spec therefore has 32-bit integers to ensure portability to languages that can't represent 64-bit integers.

None of this has anything to do with Date.prototype.getTime() returning 41 bits. Which (by the way) is all it takes for a numeric timestamp that has milliseconds. So my confusion is "What is it you are confused about?"

gforce301
  • 2,944
  • 1
  • 19
  • 24
  • let ss = Date.prototype.getTime() return has 41 bits, but it is held in 53 bits variable ss. Is this correct? – Nicolas S.Xu Apr 12 '18 at 16:13
  • Yup. 1 has 1 bit but it is also held in a 53 bit variable. I don't understand your point? – gforce301 Apr 12 '18 at 16:27
  • I was initially not clear about the concept of precision. Also I thought 41 bits exceeds 32 bits limits. But the 32 bits is Graphql limit, not JS limit which is 53 bits. Thanks a lot~ – Nicolas S.Xu Apr 12 '18 at 16:45