Skip to content

Instantly share code, notes, and snippets.

View the-fejw's full-sized avatar

FeJW the-fejw

  • Fullstack Engineer
  • CA
View GitHub Profile
const state = {
PENDING: 'pending',
FULFILLED: 'fulfilled',
REJECTED: 'rejected'
};
const isThenable = maybePromise => maybePromise && (typeof maybePromise === 'function');
class MyPromise {
constructor(executor) {
const state = {
PENDING: 'pending',
FULFILLED: 'fulfilled',
REJECTED: 'rejected'
};
class MyPromise {
constructor(executor) {
// initial state
this._state = state.PENDING;
@the-fejw
the-fejw / throttle.js
Created November 13, 2020 09:14
js throttle source example
function throttle(fn, wait) {
let timer = false;
return function() {
if (!timer) {
fn.apply(this, arguments);
timer = setTimeout(() => {
clearTimeout(timer);
timer = false;
}, wait);
function debounce(fn, wait) {
let timeout;
return function() {
if (timeout) {
clearTimeout(timeout);
timeout = undefined;
}
timeout = setTimeout(() => {
fn.apply(this, arguments);
}, wait)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Untitled benchmark</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# @param two ListNodes
# @return a ListNode
def mergeTwoLists(self, l1, l2):
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# @param head, a ListNode
# @return a ListNode
def deleteDuplicates(self, head):