Coder Social home page Coder Social logo

fastest-js-html-escape's Introduction

Fastest JavaScript HTML Escape

Companion repository for my "Fastest JavaScript HTML Escape" post.

This repository contains a web-based tool to benchmark several JavaScript HTML escape implementations and scripts to aggregate and plot the results.

Usage

Run Benchmarks (Easy Way)

Go to https://pmdn.org/fastest-js-html-escape/ and let the tool run for long enough to accumulate at least 100 results for each implementation.

Use the "Download" button on the left sidebar to download the raw benchmark results as a CSV file.

Continue to "Generate Charts".

Run Benchmarks (Hard Way)

Serve the files in public/ on a local port. The tool uses the Performance API from a web worker, and [web workers][] cannot be invoked from a file:/// URL, so you'll need to serve the files in public/ with a local HTTP server. Example:

# serve public on port 8080
$ cd ./public && python3 -m http.server 8080

View http://localhost:8080/ in your browser. The tool will run all of the benchmarks once to seed the browser's JIT, then automatically start collecting benchmark results.

Let the benchmarks run for long enough to accumulate at least 100 results for each implementation. While you're waiting, you can use the panels on the left sidebar of the to run manual tests or configure the automatic benchmarks.

After you have collected enough data, use the "Download" button on the left sidebar to download the raw benchmark results as a CSV file.

Generate Charts

Run bin/gen.rb with the path to results.csv to generate the [SVGs][] in out/.

Tip: Matplotlib generates bloated SVGs; if minify is installed, then gen.rb will use it to compress the generated SVGs.

# aggregate results from "results.csv", write charts to "out/sizes.svg"
# and "out/times.svg"
$ bin/gen.rb results.csv

If you'd like to view the intermediate aggregate results, skip gen.rb and do the following instead (again, assuming raw results in results.csv):

# aggregate results, save as "stats.csv"
$ ruby bin/agg.py < results.csv > stats.csv

# generate line plot, save to "sizes.svg"
$ python3 bin/plot-line.py < stats.csv > sizes.svg

# (optional) save minified line plot as "sizes.min.svg"
$ minify -o sizes{.min,}.svg

# generate bar plot, save to "times.svg"
$ python3 bin/plot-barh.py < stats.csv > times.svg

# (optional) save minified bar plot as "times.min.svg"
$ minify -o times{.min,}.svg

If you'd like to generate larger SVGs, the Python scripts accept an optional command-line argument. Example:

# generate 3x line plot, save to "sizes-hd.svg"
$ python3 bin/plot-line.py 3.0 < stats.csv > sizes.svg

# (optional) save minified line plot as "sizes-hd.min.svg"
$ minify -o sizes-hd{.min,}.svg

Results

My results (Lenovo Thinkpad X1 Carbon 9th Gen, Debian, 64-bit Chrome 99.0.4840.0, 2022-03-08) are shown below.

String Length vs HTML Escape Function Call Time (μs)

HTML Escape Function Call Time (μs)

fastest-js-html-escape's People

Contributors

pablotron avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

picoblitz

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.