Skip to content

Instantly share code, notes, and snippets.

View jackyef's full-sized avatar
🧊
Chilling

Jacky Efendi jackyef

🧊
Chilling
View GitHub Profile
@jackyef
jackyef / maximizing-income.js
Last active September 15, 2019 08:55
Problem solving exercise
/**
* You are an actor.
*
* You are presented with different job opportunities that each takes a specific time to
* complete. You can only work on one job opportunity at a time. After accepting a job,
* you have to finish it. Every job is defined by an 2-element-array, which defines
* when it starts and when it ends.
*
* For example, if you take job [2, 4], you can't take job [4, 5] or [1, 3].
*
<html>
<video id="video" autoplay></video>
<!-- the javascript bindings -->
<script src="qr_rust.js"></script>
<script>
wasm_bindgen("qr_rust_bg.wasm")
.then(() => {
console.log('it is loaded!');
// initialize the media stream
function captureImage() {
const { decode_qr } = wasm_bindgen;
const video = document.getElementById('video');
const canvas = document.createElement('canvas');
const scale = 0.25; // we scale the image down to improve performance
canvas.width = video.videoWidth * scale;
canvas.height = video.videoHeight * scale;
canvas
.getContext("2d")
@jackyef
jackyef / bindings.js
Created July 16, 2019 03:43
wasm bindgen generated bindings
__exports.decode_qr = function(bytes) {
const retptr = 8;
const ret = wasm.decode_qr(retptr, passArray8ToWasm(bytes), WASM_VECTOR_LEN);
const memi32 = getInt32Memory();
const v0 = getStringFromWasm(memi32[retptr / 4 + 0], memi32[retptr / 4 + 1]).slice();
wasm.__wbindgen_free(memi32[retptr / 4 + 0], memi32[retptr / 4 + 1] * 1);
return v0;
};
@jackyef
jackyef / bindings.js
Last active July 16, 2019 03:44
wasm generated binding
function init(module) {
let result;
const imports = {};
if (module instanceof URL || typeof module === 'string' || module instanceof Request) {
const response = fetch(module);
if (typeof WebAssembly.instantiateStreaming === 'function') {
result = WebAssembly.instantiateStreaming(response, imports)
.catch(e => {
console.warn("`WebAssembly.instantiateStreaming` failed. Assuming this is because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
@jackyef
jackyef / lib.rs
Created July 15, 2019 16:49
Rust Wasm medium writing
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
use image;
use rqrr;
#[wasm_bindgen]
pub fn decode_qr(bytes: &[u8]) -> String {
let img = match image::load_from_memory(&bytes) {
Ok(v) => v,
@jackyef
jackyef / Cargo.toml
Last active July 15, 2019 16:48
Rust Wasm Medium Writing
[package]
name = "qr-rust"
version = "0.1.0"
authors = ["Jacky Efendi <jacky.efendi1@gmail.com>"]
edition = "2018"
[lib]
crate-type = ["cdylib"]
[dependencies]
!(function(e) {
function t(t) {
for (var n, l, i = t[0], a = t[1], f = t[2], d = 0, p = []; d < i.length; d++)
(l = i[d]), u[l] && p.push(u[l][0]), (u[l] = 0);
for (n in a) Object.prototype.hasOwnProperty.call(a, n) && (e[n] = a[n]);
for (c && c(t); p.length; ) p.shift()();
return o.push.apply(o, f || []), r();
}
function r() {
for (var e, t = 0; t < o.length; t++) {
!(function(e) {
function t(t) {
for (var r, a, c = t[0], l = t[1], i = t[2], s = 0, p = []; s < c.length; s++)
(a = c[s]), o[a] && p.push(o[a][0]), (o[a] = 0);
for (r in l) Object.prototype.hasOwnProperty.call(l, r) && (e[r] = l[r]);
for (f && f(t); p.length; ) p.shift()();
return u.push.apply(u, i || []), n();
}
function n() {
for (var e, t = 0; t < u.length; t++) {
import React, { useState, useEffect } from 'react';
import ReactDOM from 'react-dom';
const usePerformance = initialTimestamp => {
const [timings, updateTimings] = useState([]);
let prev = initialTimestamp;
useEffect(() => {
const newTimestamp = new Date().getTime();