File size: 3,274 Bytes
b91e262
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
'use client'

import { useEffect, useRef, useState } from 'react'
import { format, measure } from '../lib/measure'

function report(result, element, textarea) {
  if (!globalThis.BENCHMARK_RESULTS) {
    globalThis.BENCHMARK_RESULTS = []
  }
  globalThis.BENCHMARK_RESULTS.push(result)

  const formattedResult = format(result)
  element.textContent += `: ${formattedResult}`
  textarea.current.value += `\n    ${formattedResult}`
  console.log(formattedResult)
  element.disabled = true
}

async function measureClientButton(element, textarea, name, fn) {
  if (element.textContent.includes('Loading time')) {
    return
  }

  const result = await measure(name, fn)
  report(result, element, textarea)
}

async function measureActionButton(element, textarea, action) {
  if (element.textContent.includes('Loading time')) {
    return
  }

  const result = await action()

  report(result, element, textarea)
}

async function measureApiButton(element, textarea, url) {
  if (element.textContent.includes('Loading time')) {
    return
  }

  const result = await fetch(url).then((res) => res.json())

  report(result, element, textarea)
}

export function Client({ prefix, commonjsAction, esmAction }) {
  const [runtime, setRuntime] = useState('')
  const textarea = useRef()
  useEffect(() => {
    setRuntime(
      `${globalThis.TURBOPACK ? 'Turbopack' : 'Webpack'} (${process.env.NODE_ENV})`
    )
  }, [])
  return (
    <>
      <h1>{runtime}</h1>
      <p>
        <button
          type="button"
          onClick={(e) =>
            measureClientButton(
              e.target,
              textarea,
              'client commonjs',
              () => import('../lib/commonjs.js')
            )
          }
        >
          CommonJs client
        </button>
      </p>
      <p>
        <button
          type="button"
          onClick={(e) =>
            measureClientButton(
              e.target,
              textarea,
              'client esm',
              () => import('../lib/esm.js')
            )
          }
        >
          ESM client
        </button>
      </p>
      {commonjsAction && (
        <p>
          <button
            type="button"
            onClick={(e) =>
              measureActionButton(e.target, textarea, commonjsAction)
            }
          >
            CommonJs server action
          </button>
        </p>
      )}
      {esmAction && (
        <p>
          <button
            type="button"
            onClick={(e) => measureActionButton(e.target, textarea, esmAction)}
          >
            ESM server action
          </button>
        </p>
      )}
      <p>
        <button
          type="button"
          onClick={(e) =>
            measureApiButton(e.target, textarea, `${prefix}/commonjs`)
          }
        >
          CommonJs API
        </button>
      </p>
      <p>
        <button
          type="button"
          onClick={(e) => measureApiButton(e.target, textarea, `${prefix}/esm`)}
        >
          ESM API
        </button>
      </p>
      {
        // holds all the timing data for easier copy paste
      }
      <textarea
        readOnly={true}
        ref={textarea}
        value={runtime}
        style={{ fieldSizing: 'content' }}
      ></textarea>
    </>
  )
}