Skip to content

Instantly share code, notes, and snippets.

View gfx's full-sized avatar

FUJI Goro gfx

View GitHub Profile
function* iterateParams() {
for (const x of ["a", "b", "c"]) {
for (const i of [1, 2, 3]) {
yield { x, i };
}
}
}
for (const p of iterateParams()) {
console.log(p);
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
int main() {
printf("gettmeofday:\n");
for (int i = 0; i < 10; i++) {
struct timeval tv1, tv2;
gettimeofday(&tv1, NULL);
gettimeofday(&tv2, NULL);
@gfx
gfx / prog.c
Created April 6, 2020 02:50
`fread(3)` returns 0 for success. why?
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
int main() {
char cmdline[256];
char proc_file[256];
snprintf(proc_file, sizeof(proc_file), "/proc/%d/cmdline", getpid());
@gfx
gfx / asroot.pm
Last active September 4, 2019 05:17
perl -Masroot foo.pl # run it as root
# usage: use asroot; # run it as root
package asroot;
use strict;
use warnings;
# TODO: inherits @INC
exec "sudo", $^X, $0, @ARGV if $< != 0;
1;
@gfx
gfx / webpack.config.js.diff
Created August 9, 2019 07:21
core-js-compat
diff --git a/config/webpack.config.js b/config/webpack.config.js
index 601977b04..63866d814 100644
--- a/config/webpack.config.js
+++ b/config/webpack.config.js
@@ -37,11 +37,16 @@ const publicPath = (() => {
const DAY_MS = 24 * 60 * 60 * 1000;
const ASSET_EXPIRED_TIME_MS = Date.now() - 14 * DAY_MS; // 14 days ago
+// filter core-js modules instead of just using "core-js" to reduce bundle sizes
+const coreJsModules = require("core-js-compat")({
@gfx
gfx / .eslintrc.js
Last active August 6, 2019 05:25
Kibela's eslint / prettier config files
module.exports = {
extends: [
// https://eslint.org/docs/rules/
"eslint:recommended",
// https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/src/configs/recommended.json
"plugin:@typescript-eslint/recommended",
// https://prettier.io/docs/en/eslint.html
"plugin:prettier/recommended",
// https://github.com/yannickcr/eslint-plugin-react
"plugin:react/recommended",
// NOTE: use the last segment of the result CSV
/* eslint-disable no-console */
import { utf8EncodeJs, utf8Count, utf8DecodeJs, utf8DecodeTD } from "../src/utils/utf8";
import { utf8DecodeWasm } from "../src/wasmFunctions";
// @ts-ignore
import Benchmark from "benchmark";
for (const baseStr of ["A"]) {
@gfx
gfx / json-bench.js
Last active June 28, 2019 04:48
Evaluating JSON.parse vs object literal. cf. https://v8.dev/blog/cost-of-javascript-2019
const fs = require("fs");
const assert = require("assert");
const jsonBuffer = fs.readFileSync("./schema.json");
const object = JSON.parse(jsonBuffer.toString());
const jsonParseExpr = `(JSON.parse(${JSON.stringify(JSON.stringify(object))}))`;
const objectLiteralExpr = `(${JSON.stringify(object)})`;
const N = 100;
@gfx
gfx / msgpack-rails-init.rb
Created June 25, 2019 06:04
Mapping MessagePack timestamp type to ActiveSupport::TimeWithZone
# for Rails v5.2 + msgpack-ruby v1.3
MessagePack::DefaultFactory.register_type(
MessagePack::Timestamp::TYPE,
ActiveSupport::TimeWithZone,
packer: -> (time) do
MessagePack::Timestamp.to_msgpack_ext(time.tv_sec, time.tv_nsec)
end,
unpacker: -> (payload) do
tv = MessagePack::Timestamp.from_msgpack_ext(payload)
// Experimental `push(...bytes)`-able buffer
// Unfortunately, it is always slower than Array<number>.
export class MutableBuffer {
private length = 0;
private buffer = new Uint8Array(32);
push(...bytes: ReadonlyArray<number>): void {
const newLength = this.length + bytes.length;