From ae4921fbb92675d2d2d20abaf0a7b7bd72fc567a Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Thu, 16 May 2024 12:00:42 +0200 Subject: [PATCH] Fix some things and improve alert boxes --- _includes/footer.liquid | 2 +- ...t-shallow-array-cloning-in-javascript.html | 8 +-- _sass/_block-alert.scss | 64 +++++++++++++++---- 3 files changed, 58 insertions(+), 16 deletions(-) diff --git a/_includes/footer.liquid b/_includes/footer.liquid index bd89a7a..8ba84cd 100644 --- a/_includes/footer.liquid +++ b/_includes/footer.liquid @@ -1,6 +1,6 @@ diff --git a/_posts/2024/2024-05-10-fast-shallow-array-cloning-in-javascript/2024-05-10-fast-shallow-array-cloning-in-javascript.html b/_posts/2024/2024-05-10-fast-shallow-array-cloning-in-javascript/2024-05-10-fast-shallow-array-cloning-in-javascript.html index eb373f2..0bbf6ec 100644 --- a/_posts/2024/2024-05-10-fast-shallow-array-cloning-in-javascript/2024-05-10-fast-shallow-array-cloning-in-javascript.html +++ b/_posts/2024/2024-05-10-fast-shallow-array-cloning-in-javascript/2024-05-10-fast-shallow-array-cloning-in-javascript.html @@ -9,12 +9,12 @@ tags: ["JavaScript", "Array", "Shallow-Clone", "Clone", "Duplicate", "ECMAScript

Searching for an answer, I stumbled on this StackOverflow question. And as usual, almost all of the answers are simply regurgitated content with nothing to back it up. So with nothing else to do on a weekend, I threw my entire knowlEdge 124.0.2478.97 at the problem, wrote a benchmarking "tool", wrote several ways to do the same, and I think I found it.

Coding a Benchmark

- + All code and information provided in this article is licensed under the 3-Clause BSD License.
Please make sure include the necessary credit in your derivative work: Copyright 2024 Michael Fabian 'Xaymar' Dirks
-

I'm not a fan of services like JSPerf or JSBench, so I wrote a generalized JavaScript benchmark. Many of them rely on third-party libraries, which tend to include to rely on more third-party libraries, and when something breaks you never know why or when it'll be fixed. So instead I wrote my own generic benchmarking function using only standard JavaScript, compatible with NodeJS and common Browsers:

+

I'm not a fan of services like JSPerf or JSBench, as many of them tend to rely on third-party libraries and include permanently running code on the side. And when something breaks you never know why or when it'll be fixed, resulting in delays that really shouldn't be necessary. So instead I wrote my own generic benchmarking function using only standard JavaScript, compatible with NodeJS and common Browsers:

{% capture "code" %}{% include_relative benchTime.mjs %}{% endcapture %}{{ code | escape }} @@ -30,7 +30,7 @@ tags: ["JavaScript", "Array", "Shallow-Clone", "Clone", "Duplicate", "ECMAScript

Running a Markbench

-These results were generated on 2024-05-16 with an AMD Ryzen 7950X3D paired with 6400Mhz RAM. It is possible that JavaScript engine developers address the shortcomings in the future and as such the results may not be comparable anymore. +These results were generated on 2024-05-16 with an AMD Ryzen 7950X3D paired with 6400Mhz RAM. It is possible that JavaScript engine developers address the shortcomings in the future and as such the results may not be comparable anymore.

Now that we have everything, we just need to run the test, and even you can do it in your Browsers' Developer Console. Just paste this in: const { default: asc } = await import("./arrayShallowClone.mjs"); for (let e of [256, 2048, 16384, 131072, 1048576]){ console.log(e); asc(e); };, hit enter/return and it should start measuring. For my own sanity, I limited myself to NodeJS V8, Firefox SpiderMonkey and Chromium V8 only.

@@ -432,7 +432,7 @@ tags: ["JavaScript", "Array", "Shallow-Clone", "Clone", "Duplicate", "ECMAScript
-

This is practically insanity, but hey - why not test it anyway. This is effectively a repeat of the previous block, but the numbers are even smaller. In NodeJS the fastest way is still array.slice(), in Edge it is Object.assign([], array), and Firefox is still cheating.

+

This is practically insanity, but hey - why not test it anyway. It's effectively a repeat of the previous block, but the numbers are even smaller. In NodeJS the fastest way is still array.slice(), in Edge it is Object.assign([], array), and Firefox is still cheating.

What is truly the fastest way to shallow clone an array?

diff --git a/_sass/_block-alert.scss b/_sass/_block-alert.scss index 0ddb786..e967306 100644 --- a/_sass/_block-alert.scss +++ b/_sass/_block-alert.scss @@ -1,19 +1,23 @@ alert.block { position: relative; display: block; + margin: .5em; margin-left: 1.5em; margin-right: 1.5em; - padding: 0.5em; + padding: .5em; padding-left: 4em; min-height: 4em; - border: 1px solid hsl($theme-hue 30% 40%); - border-radius: 1em; + border: 2px solid hsl($theme-hue 30% 40%); + border-radius: 0.2rem; background: hsl($theme-hue 20% 30%); + box-shadow: 0px 0px 2px 2px rgb(0, 0, 0); overflow: hidden; } alert::before { + content: "✅"; + position: absolute; top: 0px; bottom: 0px; @@ -23,22 +27,60 @@ alert::before { min-width: 4rem; min-height: 4rem; - font-size: 4rem; - - content: "⚠"; + font-size: 3rem; white-space: no-wrap; text-align: center; - line-height: 1em; + + display: flex; + flex-direction: row; + align-items: center; + justify-content: center; } -alert[data-type="hint"] { +alert.block[data-type*="good"] { + border-color: oklch(75.52% 0.1158 150.35); + background: oklch(54.45% 0.1158 150.35); +} +alert[data-type*="good"]::before { + content: "👍"; +} + +alert.block[data-type*="bad"] { + border-color: oklch(62.8% 0.2568 29.234); + background: oklch(52.37% 0.1576 29.234); +} +alert[data-type*="bad"]::before { + content: "👎"; +} + +alert.block[data-type*="hint"] { + border-color: oklch(69.41% 0.138 238.24); + background: oklch(49.11% 0.0785 238.24); +} +alert[data-type*="hint"]::before { content: "❔"; } -alert[data-type="warning"]::before { +alert.block[data-type*="notice"] { + border-color: oklch(69.41% 0.138 238.24); + background: oklch(49.11% 0.0785 238.24); +} +alert[data-type*="notice"]::before { + content: "🔍"; +} + +alert.block[data-type*="warning"] { + border-color: oklch(71.76% 0.187 49.76); + background: oklch(52.65% 0.113 49.76); +} +alert[data-type*="warning"]::before { content: "⚠"; } -alert[data-type="error"]::before { - content: "❌"; +alert.block[data-type*="error"] { + border-color: oklch(62.8% 0.2568 29.234); + background: oklch(52.37% 0.1576 29.234); } +alert[data-type*="error"]::before { + content: "❌"; +} \ No newline at end of file