From ca1da0ad6bfd7e7a649f2681ba769391842af884 Mon Sep 17 00:00:00 2001 From: Boshen Date: Mon, 30 Jun 2025 21:28:49 +0800 Subject: [PATCH] refactor: improve `NODEJS_BUILTINS` Using binary search on a const array is faster and cleaner, the input string can be a long path, which makes hashing slow. --- src/builtins.rs | 117 +++++++++++++++++++++++------------------------- 1 file changed, 56 insertions(+), 61 deletions(-) diff --git a/src/builtins.rs b/src/builtins.rs index 54847cd..a9142c0 100644 --- a/src/builtins.rs +++ b/src/builtins.rs @@ -1,63 +1,58 @@ -use lazy_static::lazy_static; -use std::collections::HashSet; +pub const NODEJS_BUILTINS: &[&str] = &[ + "assert", + "assert/strict", + "async_hooks", + "buffer", + "child_process", + "cluster", + "console", + "constants", + "crypto", + "dgram", + "diagnostics_channel", + "dns", + "dns/promises", + "domain", + "events", + "fs", + "fs/promises", + "http", + "http2", + "https", + "inspector", + "module", + "net", + "os", + "path", + "path/posix", + "path/win32", + "perf_hooks", + "process", + "punycode", + "querystring", + "readline", + "readline/promises", + "repl", + "stream", + "stream/consumers", + "stream/promises", + "stream/web", + "string_decoder", + "sys", + "timers", + "timers/promises", + "tls", + "trace_events", + "tty", + "url", + "util", + "util/types", + "v8", + "vm", + "worker_threads", + "zlib", +]; -lazy_static! { - static ref BUILTINS: HashSet<&'static str> = HashSet::from_iter(vec![ - "assert", - "assert/strict", - "async_hooks", - "buffer", - "child_process", - "cluster", - "console", - "constants", - "crypto", - "dgram", - "diagnostics_channel", - "dns", - "dns/promises", - "domain", - "events", - "fs", - "fs/promises", - "http", - "http2", - "https", - "inspector", - "module", - "net", - "os", - "path", - "path/posix", - "path/win32", - "perf_hooks", - "process", - "punycode", - "querystring", - "readline", - "readline/promises", - "repl", - "stream", - "stream/consumers", - "stream/promises", - "stream/web", - "string_decoder", - "sys", - "timers", - "timers/promises", - "tls", - "trace_events", - "tty", - "url", - "util", - "util/types", - "v8", - "vm", - "worker_threads", - "zlib", - ]); -} - -pub fn is_nodejs_builtin(str: &str) -> bool { - BUILTINS.contains(str) +pub fn is_nodejs_builtin(s: &str) -> bool { + NODEJS_BUILTINS.binary_search(&s).is_ok() }