Handling Legacy Environments with CoreJS Object.fromEntries
Stop using .reduce() to build objects from maps. Learn how to implement the ES2020 Object.fromEntries polyfill via CoreJS to ensure cross-browser compatibility without bloating your bundle.
04 May 2026, 01:59 UTC

The Problem: Converting Iterables to Objects
Before ES2020, converting a Map, a list of pairs, or URL search parameters into a plain JavaScript object required a manual loop or an Array.prototype.reduce() call. While Object.fromEntries() now solves this natively in modern engines, using it in a project that supports older browsers (like IE11 or early Safari versions) will trigger a runtime TypeError.
The takeaway: Use the CoreJS polyfill to provide a spec-compliant implementation of Object.fromEntries for legacy environments while leveraging tree-shaking to keep the bundle lean for modern users.
Implementing the CoreJS Polyfill
CoreJS provides a modular approach, allowing you to import only the specific feature you need rather than the entire library. To add support for Object.fromEntries, add the following import to your entry point:
import 'core-js/features/object/from-entries';
This operation modifies the global Object constructor. If the environment already supports the method, CoreJS does nothing. If it is missing, the polyfill injects a version that adheres to the ES2020 specification, ensuring that property descriptors and key types are handled consistently.
Compatibility Matrix
| Environment | Native Support | Polyfill Required |
|---|---|---|
| Chrome 74+ | Yes | No |
| Firefox 68+ | Yes | No |
| Safari 13+ | Yes | No |
| IE11 | No | Yes |
| Node 12 | No | Yes |
| Node 14+ | Yes | No |
Practical Example: URLSearchParams to Object
One of the most common use cases for Object.fromEntries is converting URLSearchParams into a usable configuration object. This is particularly useful when handling API query strings.
import 'core-js/features/object/from-entries';
// Example: Parsing a query string from a URL
const params = new URLSearchParams('theme=dark&lang=en&debug=true');
const config = Object.fromEntries(params);
console.log(config);
// Expected Output: { theme: 'dark', lang: 'en', debug: 'true' }
In this example, URLSearchParams acts as the iterable of key-value pairs. The polyfill ensures that this logic works identically in IE11 as it does in the latest version of Chrome.
Engineering Trade-offs and Limitations
While the polyfill is lightweight (approximately 2 KB gzipped), there are technical constraints to consider:
- Dependency on Symbol.iterator: The polyfill relies on
Symbol.iteratorto process the input iterable. In extremely old environments whereSymbolis not supported, this polyfill will fail unless aSymbolpolyfill is loaded first. - Bundle Bloat: If you are building a library for others to use, avoid importing the polyfill globally. Instead, use
core-js-pureto avoid polluting the global namespace of the consumer's project. - Node.js Overhead: For projects targeting Node 14+, importing this polyfill adds unnecessary code to the execution path. Use a conditional import or a build-time target (like
babel-preset-env) to exclude it for Node targets.
Verification and Testing
To verify the implementation, run the following check in your target legacy browser console or a simulated environment:
// Run this in the browser console after the bundle loads
(function() {
try {
const testMap = new Map([['key1', 'val1'], ['key2', 'val2']]);
const result = Object.fromEntries(testMap);
console.log(result.key1 === 'val1' ? 'Polyfill Active' : 'Polyfill Failed');
} catch (e) {
console.error('Object.fromEntries is not defined');
}
})();
If you are using a bundler like Webpack or Rollup, verify that tree-shaking is functioning by inspecting the production build. If your target is set to chrome 100, the core-js/features/object/from-entries code should be absent from the final bundle.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.