Cookie Consent by Free Privacy Policy Generator 📌 Mutative - 10x faster than Immer

🏠 Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeiträge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden Überblick über die wichtigsten Aspekte der IT-Sicherheit in einer sich ständig verändernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch übersetzen, erst Englisch auswählen dann wieder Deutsch!

Google Android Playstore Download Button für Team IT Security



📚 Mutative - 10x faster than Immer


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

Mutative - A JavaScript library for efficient immutable updates, 10x faster than Immer by default, even faster than naive handcrafted reducer.

Benchmark

Motivation

Writing immutable updates by hand is usually difficult, prone to errors and cumbersome. Immer helps us write simpler immutable updates with "mutative" logic.

But its performance issue causes a runtime performance overhead. Immer must have auto-freeze enabled by default(Performance will be worse if auto-freeze is disabled), such immutable state with Immer are not common. In scenarios such as cross-process, remote data transfer, etc., we have to constantly freeze these immutable data.

There are more parts that could be improved, such as better type inference, non-intrusive markup, support for more types of immutability, Safer immutability, and so on.

This is why Mutative was created.

Repo: https://github.com/unadlib/mutative

Mutative vs Immer Performance

Measure(ops/sec) to update 50K arrays and 1K objects, bigger the better(view source).

Naive handcrafted reducer - No Freeze x 3,713 ops/sec ±0.86% (89 runs sampled)
Mutative - No Freeze x 5,323 ops/sec ±1.69% (93 runs sampled)
Immer - No Freeze x 8 ops/sec ±0.88% (23 runs sampled)

Mutative - Freeze x 875 ops/sec ±1.20% (95 runs sampled)
Immer - Freeze x 320 ops/sec ±0.45% (92 runs sampled)

Mutative - Patches and No Freeze x 752 ops/sec ±0.16% (96 runs sampled)
Immer - Patches and No Freeze x 7 ops/sec ±1.32% (23 runs sampled)

Mutative - Patches and Freeze x 425 ops/sec ±0.33% (95 runs sampled)
Immer - Patches and Freeze x 239 ops/sec ±0.99% (89 runs sampled)

The fastest method is Mutative - No Freeze

Run yarn benchmark to reproduce them locally.

OS: macOS 12.6, CPU: Apple M1 Max, Node.js: 16.14.2

Immer relies on auto-freeze to be enabled, if auto-freeze is disabled, Immer will have a huge performance drop and Mutative will have a huge performance lead, especially with large data structures it will have a performance lead of more than 50x.

So if you are using Immer, you will have to enable auto-freeze for performance. Mutative allows to disable auto-freeze by default. With the default configuration of both, we can see the performance gap between Mutative (5,323 ops/sec) and Immer (320 ops/sec).

Overall, Mutative has a huge performance lead over Immer in more performance testing scenarios. Run yarn performance to get all the performance results locally.

Features and Benefits

  • Mutation makes immutable updates
  • Support apply patches
  • Optional freezing state
  • Custom shallow copy
  • Immutable and mutable data markable
  • Strict mode for safer mutable data access
  • Support for JSON patches

Mutative Size: 4.11 kB with all dependencies, minified and gzipped.

Difference between Mutative and Immer

- Mutative Immer
Custom shallow copy
Strict mode
No data freeze by default
Non-invasive marking
Complete freeze data
Non-global config
Support IE browser

Mutative draft functions don't allow return value (except for void or Promise<void>), but immer is allowed.

Mutative has fewer bugs such as accidental draft escapes than Immer, view details.

Installation

yarn install mutative # npm install mutative

Usage

import { create } from 'mutative';

const baseState = {
  foo: 'bar',
  list: [{ text: 'coding' }],
};

const state = create(baseState, (draft) => {
  draft.foo = 'foobar';
  draft.list.push({ text: 'learning' });
});

create(baseState, (draft) => void, options?: Options): newState

The first argument of create() is the base state. Mutative drafts it and passes it to the arguments of the draft function, and performs the draft mutation until the draft function finishes, then Mutative will finalize it and produce the new state.

Use create() for more advanced functions by setting options, which also supports currying.

  • strict - boolean, the default is false.

Forbid accessing non-draftable values in strict mode.

  • enablePatches - boolean, the default is false.

Enable patch, and return the patches and inversePatches.

  • enableAutoFreeze - boolean, the default is false.

Enable autoFreeze, and return frozen state.

  • mark - () => ('mutable'|'immutable'|function)

Set a mark to determine if the object is mutable or if an instance is an immutable, and it can also return a shallow copy function(AutoFreeze and Patches should both be disabled).

FAQs

  • Why doesn't Mutative support IE?

Mutative is a library that relies heavily on the use of the Proxy object, which is a feature of modern web browsers that allows the interception of various operations on objects. As such, Mutative may not be fully compatible with older browsers that do not support the Proxy object, such as Internet Explorer. However, these older browsers make up a very small percentage of the overall browser market, so the impact on compatibility is likely minimal.

  • Why does Mutative have such good performance?

Mutative optimization focus is on shallow copy optimization, more complete lazy drafts, finalization process optimization, and more.

  • I'm already using Immer, can I migrate smoothly to Mutative?

Yes. Unless you have to be compatible with Internet Explorer, Mutative supports almost all of Immer features, and you can easily migrate from Immer to Mutative.

Migration is also not possible for React Native that does not support Proxy.

Conclusion

Mutative is inspired by Immer.

Mutative aims at efficient immutable updates, focusing on performance improvements and better APIs to bring better development experience. If you think Mutative is good, feel free to give it a star!

Repo: https://github.com/unadlib/mutative

...



📌 Comparing React state tools: Mutative vs. Immer vs. reducers


📈 40.15 Punkte

📌 I made Express faster than Fastify (100x faster JSON)


📈 26.92 Punkte

📌 Do Faster Payments Mean Faster Fraud?


📈 20.02 Punkte

📌 Do Faster Payments Mean Faster Fraud?


📈 20.02 Punkte

📌 Microsoft Bounty Program Updates: Faster bounty review, faster payments, and higher rewards


📈 20.02 Punkte

📌 zoxide 0.4.1, a faster way to navigate your filesystem - now 2-3x faster


📈 20.02 Punkte

📌 Faster feedback loops make for faster developer velocity (Ep. 498)


📈 20.02 Punkte

📌 Faster Pandas: Make Your Code Run Faster and Consume Less Memory || Miki Tebeke


📈 20.02 Punkte

📌 [Nestia] Boost up your NestJS server much faster and easier (maximum 20,000x faster)


📈 20.02 Punkte

📌 [Nestia] Boost up your NestJS server much faster and easier (maximum 20,000x faster)


📈 20.02 Punkte

📌 Microsoft Bounty Program Updates: Faster bounty review, faster payments, and higher rewards


📈 20.02 Punkte

📌 Microsoft Bounty Program Updates: Faster bounty review, faster payments, and higher rewards


📈 20.02 Punkte

📌 Are We Becoming More Moral Faster Than We're Becoming More Dangerous?


📈 16.91 Punkte

📌 Are We Becoming More Moral Faster Than We're Becoming More Dangerous?


📈 16.91 Punkte

📌 UK Cybersecurity: Permanent job salaries growing faster than contractor pay rises


📈 16.91 Punkte

📌 Into The Breach: Macher von Faster Than Light mit neuem Strategiespiel


📈 16.91 Punkte

📌 OnePlus 3 Survives Aerial Drop Test, Proves to Charge Faster than the Galaxy S7


📈 16.91 Punkte

📌 4K UHD TVs Are Being Adopted Faster Than HDTVs


📈 16.91 Punkte

📌 Fake news travels faster than truth on Twitter, and we can’t blame bots


📈 16.91 Punkte

📌 Fake news travels faster than truth on Twitter, and we can’t blame bots


📈 16.91 Punkte

📌 Node.js is faster than Go at REST


📈 16.91 Punkte

📌 Command-line Tools can be 235x Faster than your Hadoop Cluster


📈 16.91 Punkte

📌 Is FreeBSD faster than Linux?


📈 16.91 Punkte

📌 Nokia Says It Can Deliver Internet 1,000x Faster Than Google Fiber


📈 16.91 Punkte

📌 Banks Adopting Blockchain 'Dramatically Faster' Than Expected


📈 16.91 Punkte

📌 When Fast is Faster Than Fastest


📈 16.91 Punkte

📌 AMD's New Threadripper 2990WX Much Faster On Linux Than Windows 10


📈 16.91 Punkte

📌 libspng 0.3.1 released - faster than libpng


📈 16.91 Punkte

📌 Why does Linux run faster than windows?


📈 16.91 Punkte

📌 D-Wave's 2,000-Qubit Quantum Annealing Computer Now 1,000x Faster Than Previous Generation


📈 16.91 Punkte

📌 Why Some iPhone 7 Models Are Faster than Others and How to Tell If Yours Is Slow


📈 16.91 Punkte

📌 Cyber Threats are emerging faster than DHS’s expectations


📈 16.91 Punkte

📌 This USB car charger on sale for $10 can charge devices faster than most


📈 16.91 Punkte











matomo