summaryrefslogtreecommitdiff
path: root/op_crates/url/benches/url_ops.rs
blob: 7d5d32879ba7a86d126f16dccc5f1356e855d3c7 (plain)
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
use bencher::{benchmark_group, benchmark_main, Bencher};

use deno_core::op_sync;
use deno_core::v8;
use deno_core::JsRuntime;

fn create_js_runtime() -> JsRuntime {
  let mut runtime = JsRuntime::new(Default::default());
  runtime.register_op("op_url_parse", op_sync(deno_url::op_url_parse));
  runtime.register_op(
    "op_url_parse_search_params",
    op_sync(deno_url::op_url_parse_search_params),
  );
  runtime.register_op(
    "op_url_stringify_search_params",
    op_sync(deno_url::op_url_stringify_search_params),
  );
  runtime.sync_ops_cache();

  runtime
    .execute(
      "bootstrap",
      "globalThis.__bootstrap = (globalThis.__bootstrap || {});",
    )
    .unwrap();
  deno_url::init(&mut runtime);
  runtime
    .execute("setup", "const { URL } = globalThis.__bootstrap.url;")
    .unwrap();

  runtime
}

pub fn bench_runtime_js(b: &mut Bencher, src: &str) {
  let mut runtime = create_js_runtime();
  let context = runtime.global_context();
  let scope = &mut v8::HandleScope::with_context(runtime.v8_isolate(), context);
  let code = v8::String::new(scope, src).unwrap();
  let script = v8::Script::compile(scope, code, None).unwrap();
  b.iter(|| {
    script.run(scope).unwrap();
  });
}

fn bench_url_parse(b: &mut Bencher) {
  bench_runtime_js(b, r#"new URL(`http://www.google.com/`);"#);
}

benchmark_group!(benches, bench_url_parse,);
benchmark_main!(benches);