Giter Site home page Giter Site logo

Comments (3)

Jadarma avatar Jadarma commented on May 19, 2024

The Instant class itself is written with discrete units in mind, since they are faster to do calculations with and easier to reason about on platforms that have the concept of longs (all targets except JS). Internally, all instants hold their data as a Long, so even if a constructor would accept a double, in order to adapt it to the common definition, it will still have to do the conversion, introducing the boxing. The only alternative would be to rewrite the JS implementation to not use longs at all, which is not an option since it would make it too different from the others, so this object creation in JavaScript is a necessary evil that shouldn't be circumvented.

I would instead suggest you define your own constructor by extending the companion object, just so at least you don't have to put a .toLong() in every place where it is called:

@Suppress("NOTHING_TO_INLINE")
inline fun Instant.Companion.fromEpochMilliseconds(epochMilliseconds: Double) =
    Instant.fromEpochMilliseconds(epochMilliseconds.toLong())

val time = Instant.fromEpochMilliseconds(12345678.9)

The real question is: have you noticed any significant performance issues in your project while using the library?
If so, are you sure it is related to the boxing?

from kotlinx-datetime.

ta834n avatar ta834n commented on May 19, 2024

Hello @Jadarma ,
thank you very much for that answer.
To answer your question: I have not checked whether there are performance issues while using the library.
We have heavily used timestamps of type Long in our project but replaced them all with Double values once we measured an performance impact. As far as I know only Int values are mapped to JavaScript Number values but they cannot be used due to their reduced range.
Since we do not want to go back to Long values I have to reevaluate for which part I will use kotlinx-datetime.

from kotlinx-datetime.

Jadarma avatar Jadarma commented on May 19, 2024

Hello, @ta834n. Glad I could help. Just a small correction: all numeric types except Long are mapped to the JS Number, see here.

from kotlinx-datetime.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.