Skip to content

Instantly share code, notes, and snippets.

@yuval-a
yuval-a / js-micro.js
Last active May 16, 2024 21:01
Javascript micro-optimizations
NOTE: This document is OLD - and most of the tips here are probably outdated, since newer versions of Javascript have been
released over the years - with newer optimizations and more emphasis on optimizing newly supported syntax.
// Array literal (= []) is faster than Array constructor (new Array())
// http://jsperf.com/new-array-vs-literal/15
var array = [];
// Object literal (={}) is faster than Object constructor (new Object())
// http://jsperf.com/new-array-vs-literal/26
@yuval-a
yuval-a / HTML_HEAD
Last active August 29, 2015 14:19
HTML Head template
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="description" content="[description here]" />
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>[title here]</title>
<link rel="stylesheet" href="css/normalize.css" />
<link rel="stylesheet" href="css/style.css" />
@yuval-a
yuval-a / smooth_scroll_jquery
Last active August 29, 2015 14:19
JQuery - Smooth scroll to any anchor
$window.load(function() {
var scrollOffset = ; // Do some padding calculations etc. - this will be substracted from the target item top position
// Assign to any links/buttons with '#something' in their href.
$('#nav a').on('click',function(e) {
var $href = $(this).attr('href'),
hi = $href.indexOf('#');
// If no hash part return;
if (hi==-1) return true;
e.preventDefault();
// The double decodeURI is to support non-english
@yuval-a
yuval-a / xmlhttp_post
Created May 2, 2015 01:20
XMLHttpRequest POST
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", url, true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert(xmlhttp.responseText);
}
}
xmlhttp.send("param=bla&param=bla");
@yuval-a
yuval-a / getjsonp
Created May 2, 2015 01:52
getJSONP in vanilla JS
function getJSONP(url, success) {
var ud = 'callback_' + + Math.floor(Math.random()*1000000),
script = document.createElement('script'),
head = document.getElementsByTagName('head')[0]
|| document.documentElement;
window[ud] = function(data) {
head.removeChild(script);
window[ud] = null;
success && success(data);
@yuval-a
yuval-a / css transitions
Created May 25, 2015 19:05
transitions with vendor prefix
-webkit-transition: prop 0.2s ease;
-moz-transition: prop 0.2s ease;
-o-transition: prop 0.2s ease;
transition: prop 0.2s ease;
@yuval-a
yuval-a / formvalidation.js
Created July 26, 2015 23:02
Form validation - one function
// put 'req' on the elements of any required input fields. Don't use the 'required' attribute, otherwise the default action
// will trigger.
function onFormSubmit(form) {
var isValid = true;
Array.prototype.forEach.call(form.elements, function (e) {
if ((e.value.length == 0 || ! /\S/g.test(e.value)) && e.getAttribute('req') !== null && e.getAttribute('req') !== undefined) {
isValid = false;
e.value = "";
if (e.getAttribute('req') == "") {
@yuval-a
yuval-a / XMLHTTP_GET_Request.js
Last active August 31, 2015 09:15
XMLHTTP GET Request
var url = '/.../';
var req = new XMLHttpRequest();
req.open('GET', url, true);
req.onreadystatechange = function () {
if (req.readyState == 4 && req.status == 200)
console.log(req.responseText);
else
console.log("Error loading page");
};
req.send();
// A quick Javascript class for creating a countdown timer
// leadingZero is a boolean, onTimeCallback will be called on each second update with the timer string
function Countdown(hours,minutes,seconds, leadingZero, onTimeCallback) {
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
this.countdownStr = hours+":"+minutes+":"+seconds;
this.onTime = onTimeCallback;
this.leadingZero = leadingZero;
@yuval-a
yuval-a / react-component-empty-class
Created January 31, 2019 17:56
React Component class "abstract" prototype
import React, { Component } from 'react'
export default class ComponentName extends Component {
constructor(props) {
console.log ("constructor");
super(props);
this.state = {
};
}