Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Enums vs string constants
(version: 0)
Comparing TS enums and string constants
Comparing performance of:
TS enum vs String constants
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
"use strict"; // Welcome to the TypeScript Playground, this is a website // which gives you a chance to write, share and learn TypeScript. // You could think of it in three ways: // // - A location to learn TypeScript where nothing can break // - A place to experiment with TypeScript syntax, and share the URLs with others // - A sandbox to experiment with different compiler features of TypeScript
Tests:
TS enum
var Foo; (function (Foo) { Foo[Foo["FOO"] = 0] = "FOO"; Foo[Foo["BAR"] = 1] = "BAR"; Foo[Foo["BAZ"] = 2] = "BAZ"; })(Foo || (Foo = {})); let enum_foo_count = 0; let enum_bar_count = 0; let enum_baz_count = 0; var getRandomEnum = () => { const r = Math.random(); if (r < 1 / 3) return Foo.FOO; if (r < 2 / 3) return Foo.BAR; return Foo.BAZ; }; for (let i = 0; i < 100000; i++) { switch (getRandomEnum()) { case Foo.FOO: enum_foo_count++; break; case Foo.BAR: enum_bar_count++; break; case Foo.BAZ: enum_baz_count++; break; } }
String constants
let string_foo_count = 0; let string_bar_count = 0; let string_baz_count = 0; var getRandomStr = () => { const r = Math.random(); if (r < 1 / 3) return "FOO"; if (r < 2 / 3) return "BAR"; return "BAZ"; }; for (let i = 0; i < 100000; i++) { switch (getRandomStr()) { case "FOO": string_foo_count++; break; case "BAR": string_bar_count++; break; case "BAZ": string_baz_count++; break; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
TS enum
String constants
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and analyze what's being tested. **Benchmark Definition** The benchmark is comparing two approaches: TypeScript enums (specifically, using an object with string keys) and string constants. **What are we testing?** We're testing the performance difference between: 1. Using a TypeScript enum (an object with named values) to select a value. 2. Using string constants directly in a switch statement. **Options compared** The benchmark is comparing two options: A) **TS enum**: Using an object with named values to represent the possible values. The `Foo` object is created, and its properties (`FOO`, `BAR`, `BAZ`) are assigned values. B) **String constants**: Directly using string literals (`"FOO"`, `"BAR"`, `"BAZ"`). **Pros and Cons** 1. **TS Enum** * Pros: + Easier to read and maintain, as the code is more concise and self-documenting. + Can be used in a more functional programming style. * Cons: + May have slower performance due to the overhead of object creation and lookup. 2. **String Constants** * Pros: + Typically faster, as string literals are cached by the browser. + Less memory usage, as no object is created. * Cons: + May be less readable, especially in large codebases. **Library and Special JS Feature** There is no explicit library mentioned in the benchmark definition. However, TypeScript enums use a feature of the language that allows creating an object with named values. The `Foo` object is defined using a technique called "self-invoking function expression" or IIFE, which creates a new scope and executes immediately when the code runs. This helps to avoid polluting the global namespace. **Other Considerations** * The benchmark uses a simple loop with 100,000 iterations to generate random values and select from the possible options. * Both test cases use a switch statement, which can be optimized by the browser's JIT compiler. **Alternatives** If you want to optimize this benchmark or compare similar approaches, consider exploring other options: 1. **Object literal**: Instead of using an object with named values (enum), try using an object literal with string keys. 2. **Switch expression**: If supported by your target browsers, switch expressions can be used instead of traditional switch statements. Keep in mind that the performance differences between these approaches may vary depending on the specific browser and JavaScript engine being used.
Related benchmarks:
Try/Catch vs Typeof
overwrite string vs null vs const two
typeof "undefined" versus falsey check
typeof !== undefined vs strict !equal
Comments
Confirm delete:
Do you really want to delete benchmark?