Skip to content

Instantly share code, notes, and snippets.

@jebai0521
Created April 14, 2020 03:41
Show Gist options
  • Save jebai0521/60542d8e1f2b20f22de08e0a29f218ca to your computer and use it in GitHub Desktop.
Save jebai0521/60542d8e1f2b20f22de08e0a29f218ca to your computer and use it in GitHub Desktop.
sha256benchmark.
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow strict-local
*/
import React from 'react';
import ReactAES from 'react-native-aes-encryption';
import {sha256 as js_sha256} from 'js-sha256';
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
Button,
} from 'react-native';
import {Colors} from 'react-native/Libraries/NewAppScreen';
import {isEqual} from 'lodash';
class App extends React.Component {
constructor(props) {
super(props);
this.state = {};
}
componentDidMount() {}
testSHA256 = async (times) => {
let jsResult, nativeResult;
const seed = 'abcdefghijklmnopqrstuvwxyz' + new Date().getTime();
let totalJs, totalNative;
let avgJs, avgNative;
const resutlJS = [],
resultNative = [];
{
let src = seed;
const start = new Date();
for (let i = 0; i < times; i++) {
src = js_sha256(src + String(i));
}
const end = new Date();
resutlJS.push(src);
console.log(resutlJS);
totalJs = end.getTime() - start.getTime();
avgJs = totalJs / times;
jsResult = `js ${times} times, total: ${totalJs}ms avg: ${avgJs}ms`;
console.log(jsResult);
}
{
let src = seed;
const start = new Date();
for (let i = 0; i < times; i++) {
src = await ReactAES.sha256(src + String(i), 128);
}
const end = new Date();
resultNative.push(src);
console.log(resultNative);
totalNative = end.getTime() - start.getTime();
avgNative = totalNative / times;
nativeResult = `native ${times} times, total ${totalNative}ms avg: ${avgNative}ms`;
console.log(nativeResult);
}
let same = true;
for (let i = 0; i < resutlJS.length; i++) {
console.log(resutlJS[i], resultNative[i]);
if (resutlJS[i] !== resultNative[i]) {
same = false;
break;
}
}
const t = avgNative / avgJs - 1;
const s = same ? 'same' : 'not same';
const compare = `js ${t} times faster than native\n results ${s}\n`;
console.log(compare);
return `${jsResult}\r\n${nativeResult}\n${compare}\n`;
};
onPressCompareSHA256 = async () => {
let results = '';
results += await this.testSHA256(1);
results += await this.testSHA256(1);
results += await this.testSHA256(1);
results += await this.testSHA256(1);
results += await this.testSHA256(10);
results += await this.testSHA256(100);
results += await this.testSHA256(1000);
results += await this.testSHA256(10000);
results += await this.testSHA256(10000);
this.setState({
shaResult: results,
});
};
render() {
const {shaResultA, shaResult, md5Result} = this.state;
return (
<>
<StatusBar barStyle="dark-content" />
<SafeAreaView>
<ScrollView
contentInsetAdjustmentBehavior="automatic"
style={styles.scrollView}>
<View style={styles.body}>
<Button
title="SHA256(Native Await)"
onPress={this.onPressCompareSHA256}
/>
<Text>{shaResult}</Text>
</View>
</ScrollView>
</SafeAreaView>
</>
);
}
}
const styles = StyleSheet.create({
scrollView: {
backgroundColor: Colors.lighter,
},
engine: {
position: 'absolute',
right: 0,
},
body: {
backgroundColor: Colors.white,
},
sectionContainer: {
marginTop: 32,
paddingHorizontal: 24,
},
sectionTitle: {
fontSize: 24,
fontWeight: '600',
color: Colors.black,
},
sectionDescription: {
marginTop: 8,
fontSize: 18,
fontWeight: '400',
color: Colors.dark,
},
highlight: {
fontWeight: '700',
},
footer: {
color: Colors.dark,
fontSize: 12,
fontWeight: '600',
padding: 4,
paddingRight: 12,
textAlign: 'right',
},
});
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment