Beta Acid logo

Upcoming JavaScript Features: Simplifying Array Combinations with Array.zip and Array.zipKeyed

Development

Daniel Moura

November 14, 2024

5 min read

In this multi-part series, we’ll explore some of the latest and most impactful proposals from the JavaScript standards body. Each post will focus on a specific proposal, providing an in-depth look at its purpose, syntax, use cases, and potential impact on the JavaScript ecosystem. Currently, these proposals are in the TC39 staging process, which guides potential features from concept to adoption. Join us as we explore these exciting developments and examine how they could shape the future of JavaScript.

  • Array.zip and Array.zipKeyed: Streamlining the process of combining arrays.
  • Atomics.pause: Improving efficiency in multi-threaded environments.
  • Error.isError: Providing a reliable method for error identification.
  • Iterator Chunking: Managing large or infinite data streams efficiently.

Array.zip and Array.zipKeyed

Proposal: https://github.com/tc39/proposal-array-zip

Stage: 1

Introduced a proposal to add Array.zip and Array.zipKeyed methods. These methods aim to simplify the synchronization and combination of multiple arrays or iterables, enhancing code readability and performance.

This is closely related to the Joint Iteration proposal, effectively bringing Iterator.zip and Iterator.zipKeyed functionalities to arrays. By understanding this proposal, developers can anticipate and leverage upcoming features that make array manipulation more intuitive.

Problem Statement

Despite the existence of iterators in JavaScript for over a decade, arrays remain the go-to data structure for most developers, especially for small and finite lists. Arrays are simple to work with, performant, and widely supported across the ecosystem. However, combining multiple arrays element-wise is not as straightforward as it could be.

Currently, merging arrays requires methods like Array.prototype.map, for loops, or external libraries such as Lodash. These approaches can be verbose, less ergonomic, and may introduce performance overhead.

For example, to merge two arrays element-wise:

While functional, this code isn't as clean or expressive as developers might prefer. There's a clear gap in JavaScript's array handling capabilities when it comes to synchronizing iteration over multiple arrays.

The Array.zip and Array.zipKeyed methods are based on the Joint Iteration proposal. The Joint Iteration proposal introduces Iterator.zip and Iterator.zipKeyed to synchronize the iteration of multiple iterables.

By bringing these functionalities to arrays, the Array.zip proposal acknowledges the prevalent use of arrays over iterators in everyday JavaScript development. It offers the same powerful iteration synchronization but in a form that's more familiar and ergonomic for array users.

Syntax and Examples

Array.zip

The Array.zip method creates a new array by synchronously iterating over multiple input arrays (or iterables) and combining their elements into tuples.

Syntax

Parameters

  • iterables: The arrays or iterables to zip together.

Example

Array.zipKeyed

The Array.zipKeyed method synchronously iterates over multiple arrays (or iterables) and combines their elements into objects using a provided array of keys.

Syntax

Parameters

  • keys: An array of strings representing the keys for the resulting objects.
  • iterables: The arrays or iterables whose elements will become the values in the resulting objects.

Example

Use Cases

Data Merging: When receiving separate arrays from different API endpoints that need to be combined into a single data structure.

CSV Parsing: After splitting CSV data into arrays, you can zip headers and values to create objects.

Form Handling: Combining arrays of form field names and values into objects for processing or submission.

Parallel Iteration: Simplifying the iteration over multiple arrays where elements at the same index are related.

Potential Pitfalls

Uneven Array Lengths

If the input arrays are of different lengths, Array.zip will only combine elements up to the length of the shortest array. The extra elements in longer arrays will be ignored.

Example

Solution: Ensure that all arrays are of the same length or handle the length discrepancies before zipping.

Key Mismatch in Array.zipKeyed

If the number of keys doesn't match the number of arrays provided, the method may not work as expected.

Example

Solution: Ensure the number of keys matches the number of arrays to prevent missing or undefined properties.

Not Yet Standardized

Array.zip and Array.zipKeyed are still proposals and not part of the official ECMAScript standard. They may not be available in all JavaScript environments.

Conclusion

The Array.zip and Array.zipKeyed proposal offers a more ergonomic and efficient way to synchronize and combine multiple arrays or iterables in JavaScript. By aligning with the Joint Iteration proposal, it brings powerful iteration capabilities to arrays, which are more commonly used than iterators in day-to-day coding.

Embracing these features can lead to cleaner codebases and more efficient data manipulation. Whether you're merging datasets, mapping keys to values, or performing parallel iterations, Array.zip and Array.zipKeyed offer intuitive solutions that enhance productivity.

Stay tuned for the next part of our series, where we'll explore how Atomics.pause can boost multithreaded performance in JavaScript.

SHARE THIS STORY

Get. Shit. Done. 👊

Whether your ideas are big or small, we know you want it built yesterday. With decades of experience working at, or with, startups, we know how to get things built fast, without compromising scalability and quality.

Get in touch

Whether your plans are big or small, together, we'll get it done.

Let's get a conversation going. Shoot an email over to projects@betaacid.co, or do things the old fashioned way and fill out the handy dandy form below.

Beta Acid is a software development agency based in New York City and Barcelona.


hi@betaacid.co

About us
locations

New York City

77 Sands St, Brooklyn, NY

Barcelona

C/ Calàbria 149, Entresòl, 1ª, Barcelona, Spain

London

90 York Way, London, UK

© 2024 Beta Acid, Inc. All rights reserved.