Skip to content

Instantly share code, notes, and snippets.

View bmeurer's full-sized avatar

Benedikt Meurer bmeurer

View GitHub Profile
let a = Object.assign({}, {x:1, y:2, z:3});
let b = Object.assign({}, a);
console.log("a is", a);
console.log("b is", b);
console.log("a and b have same map:", %HaveSameMap(a, b));
// Micro-benchmark to answer the question in https://twitter.com/thejameskyle/status/905403367949647874
if (typeof console === 'undefined') console = {log:print};
var closuresOriginal = (function() {
const outer = a => {
const inner = b => a + b;
return inner(2);
};
@bmeurer
bmeurer / mathias.js
Created September 20, 2017 20:55
Following up on discussion at @munichjs how to create packed arrays pre-initialized in V8
"use strict";
// These are examples how to created PACKED_*_ELEMENTS arrays preinitialized
// in V8, following up on offline discussion at the last MunichJS meetup.
function createPackedViaArrayFrom(length, value) {
return Array.from.call(null, Array.prototype.map.call({length}, _ => value));
}
function createPackedViaGenerator(length, value) {
@bmeurer
bmeurer / bench-event-emitter.js
Last active October 8, 2017 09:30
Isolated test case for the discussion around rest parameters for Node's EventEmitter (https://github.com/nodejs/node/pull/13155#issuecomment-334706721)
// Isolated test case for the discussion around rest parameters for Node's EventEmitter
// https://github.com/nodejs/node/pull/13155#issuecomment-334706721
if (typeof console === 'undefined') console = {log:print};
const N = 2e7;
const TESTS = [];
(function() {
function emitNone(handler, isFn, self) {
if (isFn)
// Benchmark to measure loops running until OOB vs. loops that run
// to length. See
//
// https://v8project.blogspot.de/2017/09/elements-kinds-in-v8.html#avoid-reading-beyond-length
//
// for background. While OOB access is now no longer almost 10x
// slower in V8, it's still an anti-pattern that should be avoided.
"use strict";
// Benchmark for the PR https://github.com/babel/babel/pull/6748
"use strict";
// Object with no enumerable properties in the prototype chain.
const a = {x:1, y:2, z:3};
// Object with enumerable properties in the prototype chain.
function B() { this.x = 1; this.y = 2; this.z = 3; }
B.prototype.foo = 4;
// Microbenchmark to investigate https://twitter.com/DasSurma/status/927324384238473222
var isEvenNum = (num = 1) => num !== 0 && num % 2 === 0;
var sqrNum = num => num * num;
var N = 1000;
var arr = [];
for (n = 1; n <= 10000; ++n) arr.push(n);
function multiloops() {
@bmeurer
bmeurer / prerender-global-_serializeOne.diff
Last active January 28, 2019 10:55
Angular Universal Prerendering test case
--- dist/prerender_ORIG.js 2019-01-28 11:32:34.000000000 +0100
+++ dist/prerender.js 2019-01-28 11:33:58.000000000 +0100
@@ -88604,66 +88604,7 @@
serialize: { value: function() {
var s = '';
for (var kid = this.firstChild; kid !== null; kid = kid.nextSibling) {
- s += kid._serializeOne(this);
- }
- return s;
- }},
@bmeurer
bmeurer / bench-instanceof-versus-typeof-megamorphic.js
Created January 28, 2019 15:10
Demonstrate the performance impact of using `instanceof` vs. checking for some existing property (in the megamorphic case)
// Copyright 2013-2019 Benedikt Meurer
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// <https://www.apache.org/licenses/LICENSE-2.0>
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// Copyright 2013-2019 Benedikt Meurer
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// <https://www.apache.org/licenses/LICENSE-2.0>
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,