# Call JS function from Kotlin

**URL:** <https://discuss.kotlinlang.org/t/call-js-function-from-kotlin/14186>\
**Category:** JavaScript\
**Created:** [September 7, 2019, 10:08am UTC](https://discuss.kotlinlang.org/t/call-js-function-from-kotlin/14186 "2019-09-07T10:08:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![qwerty.1](https://avatars.discourse-cdn.com/v4/letter/q/f6c823/32.png) [@qwerty.1](https://discuss.kotlinlang.org/u/qwerty.1)\
**Post date:** [September 7, 2019, 10:08am UTC](https://discuss.kotlinlang.org/t/call-js-function-from-kotlin/14186/1 "2019-09-07T10:08:45Z")

</div>

I’m trying to compile kotlin to js. Code:

```
import org.w3c.performance.Performance

fun main(args: Array<String>) {
    val start = Performance().now()
    
   // do stuff

    val end = Performance().now()
}

```

Then `/path/to/kotlin/compiler/bin/kotlinc-js ./MyFile.kt -output run.js` throws

```
error: cannot create an instance of an abstract class
    val end = Performance().now()

```

How to call js implementation of Performace properly?

---

<div class="post-metadata">

**Author:** ![lamba92](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lamba92/32/5089_2.png) [@lamba92](https://discuss.kotlinlang.org/u/lamba92)\
**Post date:** [September 7, 2019, 10:46am UTC](https://discuss.kotlinlang.org/t/call-js-function-from-kotlin/14186/2 "2019-09-07T10:46:35Z")

</div>

I have no idea how to resolve this problem but you could use the [Kotlin Duration stdlib API!](https://blog.jetbrains.com/kotlin/2019/08/kotlin-1-3-50-released/)

```kotlin
val (value, duration) = measureTimedValue {
    Thread.sleep(100)
    42
}

```

---

<div class="post-metadata">

**Author:** ![fatjoe79](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fatjoe79/32/4221_2.png) [@fatjoe79](https://discuss.kotlinlang.org/u/fatjoe79)\
**Post date:** [September 7, 2019, 10:54am UTC](https://discuss.kotlinlang.org/t/call-js-function-from-kotlin/14186/3 "2019-09-07T10:54:22Z")

</div>

I don’t know that API, but it looks like `Performance.now()` could work.

---

<div class="post-metadata">

**Author:** ![Varia](https://avatars.discourse-cdn.com/v4/letter/v/b9e5f3/32.png) [@Varia](https://discuss.kotlinlang.org/u/Varia)\
**Post date:** [September 7, 2019, 11:15am UTC](https://discuss.kotlinlang.org/t/call-js-function-from-kotlin/14186/4 "2019-09-07T11:15:24Z")

</div>

The `Window` object has a property `performance` of type `Performance`.

---

<div class="post-metadata">

**Author:** ![SackCastellon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sackcastellon/32/4553_2.png) [@SackCastellon](https://discuss.kotlinlang.org/u/SackCastellon)\
**Post date:** [September 7, 2019, 11:18am UTC](https://discuss.kotlinlang.org/t/call-js-function-from-kotlin/14186/5 "2019-09-07T11:18:05Z")

</div>

According to the [documentation](https://developer.mozilla.org/en-US/docs/Web/API/Performance) of the `Performance` interface:

> An object of this type can be obtained by calling the [`window.performance`](https://developer.mozilla.org/en-US/docs/Web/API/Window/performance) read-only attribute.

So your code should look something like this:

```auto
import kotlin.browser.window

fun main() {
    val start = window.performance.now()

    // do stuff

    val end = window.performance.now()
}

```
