Skip to content

Commit 0fdc88b

Browse files
committedJul 16, 2024··
perf(linter): optimize no-dupe-keys (#4292)
This tweaks the `no-dupe-keys` lint in order to try to optimize it. The lints reliably shows up in both CPU and memory profiling, due to the cost of allocating/growing the hashmap and hashing into it. This PR tries to tackle both by skipping trivial cases and by pre-allocating a larger hashmap.
1 parent c5731a5 commit 0fdc88b

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed
 

‎crates/oxc_linter/src/rules/eslint/no_dupe_keys.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use oxc_ast::{
55
use oxc_diagnostics::OxcDiagnostic;
66
use oxc_macros::declare_oxc_lint;
77
use oxc_span::{GetSpan, Span};
8-
use rustc_hash::FxHashMap;
8+
use rustc_hash::{FxBuildHasher, FxHashMap};
99

1010
use crate::{context::LintContext, rule::Rule, AstNode};
1111

@@ -43,7 +43,11 @@ impl Rule for NoDupeKeys {
4343
let AstKind::ObjectExpression(obj_expr) = node.kind() else {
4444
return;
4545
};
46-
let mut map = FxHashMap::default();
46+
let len = obj_expr.properties.len();
47+
if len <= 1 {
48+
return;
49+
}
50+
let mut map = FxHashMap::with_capacity_and_hasher(len, FxBuildHasher);
4751
for prop in &obj_expr.properties {
4852
let ObjectPropertyKind::ObjectProperty(prop) = prop else {
4953
continue;

0 commit comments

Comments
 (0)
Please sign in to comment.